在键盘按钮上完成后,将光标焦点放在Android中的其他编辑文本上



我在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但光标仍专注于edtwo。 如何更改客户焦点。

你可以试试这个:

  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;
        }
    });
你可以

这样做:

YourEditext.requestFocus();

只需调用它,它将在完成或隐藏键盘后再次将焦点重置为 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;
    }
});

相关内容

最新更新