0

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

我试过:

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" />
4

1 回答 1

2

实际上这里发生的事情是你所有的EditText人一直在监听关键事件无论他们是否处于焦点

您需要使用OnEditorActionListenerDone/Enter/Go/Search 键

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //do something
        }
    return false;
    }
});
于 2012-05-25T09:33:43.657 回答