在EditText上设置Focus()的奇怪行为

时间:2012-05-25 09:28:28

标签: android focus android-edittext

所以我有三个EditText框,当用户按下其中一个时,我想继续下一个。

我试过了:

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

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

            findViewById(R.id.editText2).requestFocus();
          return true;
        }
        return false;
    }
});


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

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

            findViewById(R.id.editText3).requestFocus();
          return true;
        }
        return false;
    }
});



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

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

            findViewById(R.id.button1).requestFocus();
          return true;
        }
        return false;
    }
});

但是使用此代码,焦点会从键盘上的editText1跳转到editText3。

这是什么(我偶然发现):

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

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

            findViewById(R.id.editText2).requestFocus();
          return true;
        }
        return false;
    }
});


final EditText pass = (EditText) findViewById(R.id.editText2);
email.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {

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

            findViewById(R.id.editText3).requestFocus();
          return true;
        }
        return false;
    }
});



final EditText passrep = (EditText) findViewById(R.id.editText3);
email.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {

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

            findViewById(R.id.button1).requestFocus();
          return true;
        }
        return false;
    }
});
例如,为同一个EditText(电子邮件)设置三个不同的OnKeyListener。

我在这里缺少什么?这是我的布局:

<EditText
    android:id="@+id/editText1"
    android:layout_width="234dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:ems="10"
    android:inputType="textEmailAddress"
    android:layout_marginTop="20dp"
    android:hint="Email" >

</EditText>

<EditText
    android:id="@+id/editText2"
    android:layout_width="234dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:ems="10"
    android:inputType="textPassword"
    android:layout_marginTop="20dp"
    android:hint="Password" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="234dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:ems="10"
    android:inputType="textPassword"
    android:layout_marginTop="20dp"
    android:hint="Confirm Password" />

<Button
    android:id="@+id/button1"
    android:layout_width="123dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="35dp"
    android:text="@string/submit" />

1 个答案:

答案 0 :(得分:2)

实际上,这里发生的是EditText所有OnEditorActionListener正在监听关键事件所有时间是否关注焦点

您需要使用editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if(actionId==EditorInfo.IME_ACTION_DONE){ //do something } return false; } }); 来完成/输入/转到/搜索键

{{1}}