【问题标题】:set cursor focus on other edittext in android after done on keyboard button在键盘按钮上完成后将光标焦点设置在android中的其他edittext上
【发布时间】:2018-01-10 13:52:55
【问题描述】:

我在 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。如何改变光标焦点。

【问题讨论】:

    标签: android android-edittext


    【解决方案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;
            }
    
        });
    

    【讨论】:

    • 尝试一下,但在完成逻辑后将默认光标焦点移到 edtwo
    • 你想在第二个editext执行完这个动作然后清空editetxt、隐藏键盘并自动设置焦点第一个editext对吗?
    • @Neil 然后上面的代码运行良好,我已经检查过了。
    【解决方案2】:

    你可以这样做-:

    YourEditext.requestFocus();
    

    【讨论】:

    • 如果codition自动将焦点设置为edtwo,请在完成方法调用后尝试一下
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-29
      相关资源
      最近更新 更多