Handle enter key in EditText across different devices

前端 未结 3 1292
温柔的废话
温柔的废话 2021-02-14 11:07

Right now I\'m handling the enter key in my EditText fields using a an onEditorActionListener and looking at the Action ID for IME_NULL. It works fine for all my users except o

3条回答
  •  孤城傲影
    2021-02-14 11:35

    Try this solution: (I haven't tested it)

    Set the following property to your EditText

    android:imeOptions="actionNext"
    

    Now you can set the following onEditorAction

    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_NEXT) {
            // Program Logic Here
            return true;    
        }
        return false;
    }
    

    For some additional functionality, you can set your password EditText to:

    android:imeOptions="actionDone"
    

    So you can then just have something like this:

    TextView.OnEditorActionListener keyListener = new TextView.OnEditorActionListener(){
            public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_NEXT) {
                    ((EditText) findViewById(R.id.etPass)).requestFocus();
                }
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    logon();
                }
                return true;
            }
        };
    

提交回复
热议问题