Handle enter key in EditText across different devices

陌路散爱 提交于 2019-12-03 12:06:17

I figured out how to get it to work.

I had to add android:singleLine="true" to the EditText tag in the layout XML (alternately you can set it by using setSingleLine() in code). This forces the edit text to use only one line and focus will go to the next EditText box.

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;
        }
    };
(EditText) passwordView = (EditText) findViewById(R.id.password);
passwordView.setImeOptions(EditorInfo.IME_ACTION_DONE);
    passwordView.setOnEditorActionListener(new OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
            {
                String input;
                if(actionId == EditorInfo.IME_ACTION_DONE)
                {
                   input= v.getText().toString();
                    Toast toast= Toast.makeText(LogIn.this,input,
                            Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    return true;
                }
                return false;
            }
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!