Android:将键盘隐藏在覆盖的“完成”中EditText 的按键

发布于 2024-08-29 06:35:55 字数 518 浏览 2 评论 0原文

我使用了一些 Android 代码来覆盖 EditText 字段中的“完成”按钮:

   myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                mySubroutine();

                return true;
            }
            return false;
        }
    });

激活该字段会调用键盘,然后按“完成”即可成功评估 mySubroutine()。但是,当我按“完成”时,键盘不再消失。如何将这种默认行为恢复到例程中?

I have used a bit of Android code to override the "Done" button in my EditText field:

   myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {

                mySubroutine();

                return true;
            }
            return false;
        }
    });

Activating the field calls up the keyboard, and pressing "Done" evaluates mySubroutine() successfully. However, the keyboard no longer goes away when I press "Done". How do I restore this default behaviour to the routine?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天冷不及心凉 2024-09-05 06:35:55

为什么不:

myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
        if (actionId == EditorInfo.IME_ACTION_DONE) { 

            mySubroutine(); 
        } 
        return false; 
    } 
}); 

在处理代码后返回 false 即可。这可以解释为无论您的代码 (mySubroutine()) 做什么,之后它仍然会使用默认操作。如果您返回“true”,则表明您是一个快乐的编码员,并且需要完成的所有操作都已在您的 mySubroutine() 中发生,并且默认操作不需要执行操作。

Why not:

myEditField.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
        if (actionId == EditorInfo.IME_ACTION_DONE) { 

            mySubroutine(); 
        } 
        return false; 
    } 
}); 

Just return false after you handle your code. This can be interpreted as no matter what your code (mySubroutine()) does it will still use the default action afterwards. If you return "true" you are telling that you are a happy coder and everything that needed to be done has happen in your mySubroutine() and the default action do not need to take action.

冷月断魂刀 2024-09-05 06:35:55

您可以通过执行以下操作来关闭键盘:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);

You can close the keyboard by doing:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);
过期情话 2024-09-05 06:35:55

您必须将 onClickListener 附加到执行以下代码的按钮:

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editview.getWindowToken(), 0);

You must attach an onClickListener to the button that executes the following code:

InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editview.getWindowToken(), 0);
始终不够 2024-09-05 06:35:55

我也有同样的问题。在 editText VISIBILITY 从 GONE 更改为 VISIBLE 后,我必须立即设置焦点并显示软键盘。我使用以下代码实现了这一点:

        (new Handler()).postDelayed(new Runnable() {

        public void run() {              yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
            yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));                       

        }
    }, 200);

I had the same problem. Immediately after editText VISIBILITY change from GONE to VISIBLE, I had to set the focus and display the soft keyboard. I achieved this using the following code:

        (new Handler()).postDelayed(new Runnable() {

        public void run() {              yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN , 0, 0, 0));
            yourEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP , 0, 0, 0));                       

        }
    }, 200);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文