Edittext 集中在第一个列表视图项中
Edittext focused in first listview item
当我的 activity 启动时,edittext 焦点始终在列表视图的最后一项上。我需要得到这个:
1. Edittext focus is always on the first listview item
2. It shows soft input keyboard automatically
这是我的主要代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] items = {"Item1","Item2","Item3",..,"Item30"};
//showing the list
final ListView listView = (ListView)findViewById(R.id.listSelected);
ArrayAdapter<String> adapter = new LVAdapter(this, items);
listView.setAdapter(adapter);
//how do I implement the code..?
LV适配器代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main_item, parent, false);
TextView textView = (TextView)rowView.findViewById(R.id.textViewItem);
textView.setText(values[position]);
return rowView;
}
}
主要xml
<ListView
android:id="@+id/listSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSelect" >
</ListView>
<Button
android:id="@+id/btnSelect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select"
android:layout_alignParentTop="true" >
</Button>
这是我的 main_item xml
<TextView
android:id="@+id/textViewItem"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<EditText
android:id="@+id/editTextInput"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<requestFocus />
</EditText>
main.xml 和 main_item.xml 都在使用 RelativeLayout。提前致谢。
从 xml
中的 EditText 移除请求焦点
<TextView
android:id="@+id/textViewItem"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<EditText
android:id="@+id/editTextInput"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
并在 position == 0
时以编程方式请求它
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main_item, parent, false);
TextView textView = (TextView)rowView.findViewById(R.id.textViewItem);
textView.setText(values[position]);
if(position == 0){
EditText etText = (EditText) rowView.findViewById(R.id.editTextInput);
etText.requestFocus();
}
return rowView;
}
当我的 activity 启动时,edittext 焦点始终在列表视图的最后一项上。我需要得到这个:
1. Edittext focus is always on the first listview item
2. It shows soft input keyboard automatically
这是我的主要代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] items = {"Item1","Item2","Item3",..,"Item30"};
//showing the list
final ListView listView = (ListView)findViewById(R.id.listSelected);
ArrayAdapter<String> adapter = new LVAdapter(this, items);
listView.setAdapter(adapter);
//how do I implement the code..?
LV适配器代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main_item, parent, false);
TextView textView = (TextView)rowView.findViewById(R.id.textViewItem);
textView.setText(values[position]);
return rowView;
}
}
主要xml
<ListView
android:id="@+id/listSelected"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnSelect" >
</ListView>
<Button
android:id="@+id/btnSelect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Select"
android:layout_alignParentTop="true" >
</Button>
这是我的 main_item xml
<TextView
android:id="@+id/textViewItem"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<EditText
android:id="@+id/editTextInput"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" >
<requestFocus />
</EditText>
main.xml 和 main_item.xml 都在使用 RelativeLayout。提前致谢。
从 xml
中的 EditText 移除请求焦点<TextView
android:id="@+id/textViewItem"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<EditText
android:id="@+id/editTextInput"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
并在 position == 0
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.main_item, parent, false);
TextView textView = (TextView)rowView.findViewById(R.id.textViewItem);
textView.setText(values[position]);
if(position == 0){
EditText etText = (EditText) rowView.findViewById(R.id.editTextInput);
etText.requestFocus();
}
return rowView;
}