android集焦点出错

时间:2014-11-27 11:48:12

标签: android focus

我创建了一个包含三个 EditText

的活动

当我按下#34;输入键"在 edittext1 中,焦点必须更改为 edittext2。

这是我的代码

 final EditText e1 = (EditText) findViewById(R.id.edit1);
 final EditText e2 = (EditText) findViewById(R.id.edit2);
        e1.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    e2.requestFocus();
                    return true;
                }
                return false;
            }
        });

但是,重点是改为 edittext3

我能做什么?


我尝试了一种解决方案,但没有工作 我的代码xml是

<EditText
    android:id="@+id/edit1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/hello"
    android:layout_marginTop="16dp"
    android:ems="10"
     >

    <requestFocus />
</EditText>

<TextView
    android:id="@+id/hello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="24dp"
    android:layout_marginTop="53dp"
    android:text="@string/location_text" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/editText2"
    android:layout_marginTop="28dp"
    android:text="@string/barcode_text" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/edit1"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:inputType="numberDecimal" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:lines="1"
    android:maxLines="1"
    android:minLines="1" />

当我输入&#34;输入&#34;在edit1中,焦点更改为editText3而不是editText2

3 个答案:

答案 0 :(得分:2)

first_edit_text属性

下的XML中定义以下内容
android:imeOptions="actionNext"
android:nextFocusDown="@+id/second_edit_text"

实施例,

<EditText
    android:id="first_edit_text"
    .
    .
    .
    android:imeOptions="actionNext"
    android:nextFocusDown="@+id/second_edit_text"
   />

<EditText
    android:id="second_edit_text"
    .
    .
    .
    />

这将使用XML设置编辑文本的下一个焦点视图。我希望它有所帮助!

<强>更新

针对您的具体情况,请将属性添加到EditText1,如下所示

<EditText
    android:id="@+id/edit1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/hello"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:imeOptions="actionNext"
    android:nextFocusDown="@+id/editText2"
     >

    <requestFocus />
</EditText>

答案 1 :(得分:0)

 <EditText
        android:id="@+id/edit1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/edit2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

在xml中这样做。工作正常.noo nedd可以添加任何额外的代码。

在活动中使用你的code.it工作正常..

答案 2 :(得分:0)

试试这个,

edtName.setNextFocusDownId(R.id.edtAddress);

        edtName.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                edtName.clearFocus();
                edtAddress.requestFocus();
                return true;
            }
            return false;
        }
    });