在键盘按钮上完成后,将光标焦点设置在android中的其他edittext上

时间:2018-01-10 05:41:10

标签: android android-edittext

我在xml文件中有以下视图

-Linearlayout -Edittext (edone) -Edittext (edtwo)

Java文件中的代码

edtwo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {

                    edtwo.setText("");
                    edone.setText("");
                    InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

                    edtwo.requestFocus();
                }

                return true;
            }

            return false;
        }
    });

点击键盘上的完成按钮后,光标想要移动到edone edittext字段,但剩余光标焦点在edtwo上。如何改变cusor焦点。

3 个答案:

答案 0 :(得分:1)

你可以尝试这个:

  edtwo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            edtwo.setText("");
            edone.setText("");
            InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

            edone.requestFocus();
            return true;
        }

    });

答案 1 :(得分:0)

只需调用它,它会在完成或键盘隐藏后再次将焦点重置为edone。

 edtwo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // do your stuff here
             InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
             imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
             edone.requestFocus();
        }
        return false;
    }
});

答案 2 :(得分:0)

你可以这样做 - :

YourEditext.requestFocus();