Edittext聚焦在第一个listview项目中

时间:2015-08-08 11:39:59

标签: android listview android-edittext focus

当我的活动开始时,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..?

LVAdapter代码:

    @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;
    }
}

main 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。提前谢谢。

1 个答案:

答案 0 :(得分:0)

从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;
}