【问题标题】:Close virtual keyboard on button press按下按钮时关闭虚拟键盘
【发布时间】:2023-03-10 11:34:01
【问题描述】:

我有一个Activity 和一个EditText、一个按钮和一个ListView。目的是在EditText 中键入搜索屏幕,按下按钮并让搜索结果填充此列表。

这一切都很好,但虚拟键盘的行为很奇怪。

如果我点击EditText,我会得到虚拟键盘。如果我单击虚拟键盘上的“完成”按钮,它就会消失。但是,如果我在单击虚拟键盘上的“完成”之前单击搜索按钮,则虚拟键盘会保留并且我无法摆脱它。单击“完成”按钮不会关闭键盘。它将“完成”按钮从“完成”更改为箭头并保持可见。

感谢您的帮助

【问题讨论】:

    标签: android virtual-keyboard


    【解决方案1】:
    InputMethodManager inputManager = (InputMethodManager)
                                      getSystemService(Context.INPUT_METHOD_SERVICE); 
    
    inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                                         InputMethodManager.HIDE_NOT_ALWAYS);
    

    我把它放在onClick(View v) 事件之后。

    需要导入android.view.inputmethod.InputMethodManager

    点击按钮时键盘隐藏。

    【讨论】:

    • 注意:(如果您想在可能没有焦点的情况下使用此方法(例如 onPause() 等):inputManager.hideSoftInputFromWindow((null == getCurrentFocus()) ? null : getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    • 你还得导入上下文。
    • 注意:如果键盘已经隐藏,则抛出 NPE。按照彼得的评论来避免这种情况。
    • 为什么在单击不相关的按钮后出现键盘?有人可以提供一些解释或链接吗?
    • 像魅力一样工作!
    【解决方案2】:
    mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // hide virtual keyboard
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(), 
                                          InputMethodManager.RESULT_UNCHANGED_SHOWN);
                return true;
            }
            return false;
        }
    });
    

    【讨论】:

    • 它对我有用。谢谢!
    【解决方案3】:

    使用下面的代码

    your_button_id.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try  {
                InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            } catch (Exception e) {
    
            }
        }
    });
    

    【讨论】:

    • 捕获异常而不是简单的空检查,认真的吗?!
    • 为我工作,在按钮点击时隐藏键盘
    • 我需要的简单解决方案:点击搜索按钮后隐藏键盘。
    • 工作没有任何问题。
    【解决方案4】:

    您应该为您的 EditView 实现 OnEditorActionListener

    public void performClickOnDone(EditView editView, final View button){
        textView.setOnEditorActionListener(new OnEditorActionListener() {
    
            @Override
            public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
                hideKeyboard();
                button.requestFocus();
                button.performClick();
                return true;
            }
        });
    

    然后你通过以下方式隐藏键盘:

    public void hideKeybord(View view) {
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),
                                      InputMethodManager.RESULT_UNCHANGED_SHOWN);
    }
    

    您还应该使用onClickListener 触发隐藏在按钮中的键盘

    现在点击虚拟键盘和按钮上的“完成”将执行相同的操作 - 隐藏键盘并执行点击操作。

    【讨论】:

    • 非常好,但我不太关注。我认为该帖子吃了一些代码(示例中的“公共无效”之后没有任何内容)。我试图在我的 Activity 的 onCreate 方法中 setOnEditorActionListner,但它不知道 setOnEditorActionListener 是什么。我收到“匿名内部类型”通知。 (我在我的 Activity onCreate 方法中这样做)i37.tinypic.com/6ozkig.png
    • 看起来这段代码有几个错误,但这是正确的想法。一方面,OnEditorActionListener 接口是一个内部类,因此您需要显式导入它(在这种情况下 Eclipse 不会为您执行此操作)或将其称为 TextView.OnEditorActionListener
    • 我遇到了一些麻烦。我已经实现了 onEditorActionListener(公共类 SearchActivity 扩展 ListActivity 实现 OnClickListener、OnEditorActionListener),我已经将一个侦听器附加到我的 EditText(mSearchText.setOnEditorActionListener(this);),但 Eclipse 不允许我覆盖 onEditorAction 处理程序(公共布尔 onEditorAction (TextView v,int actionId,KeyEvent 事件))。它说它必须重写超类方法。有什么想法吗?
    • 您好,您可以通过键入 yourEditView.setOnEditorActionListener(new OnEditorActionListener() {.... 来内联您的 OnEditorActionListener
    【解决方案5】:

    对于活动,

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

    对于片段,使用 getActivity()

    getActivity().getSystemService();

    getActivity().getCurrentFocus();

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

    【讨论】:

      【解决方案6】:

      在您的情况下,由于您只有一个 EditText,我相信下面的解决方案是最好的解决方案。如果您有多个 EditText 组件,那么您需要专注于要关闭的 EditText,或者为每个 EditText 调用波纹管函数。不过对你来说,这非常有效:

      editText.onEditorAction(EditorInfo.IME_ACTION_DONE);
      

      基本上,EditText 已经有一个函数,用于处理使用键盘后要做什么。我们只是传递我们想要关闭键盘。

      【讨论】:

      • 您能否解释一下您的解决方案为何有效,以便其他人能够理解并从中学习?谢谢!
      • 当然肖恩。我刚刚编辑了顶部,因为我是新手,如果不清楚,请告诉我,我将使用 onclick 按钮展开
      • 相信我,这是最优雅的方式之一。谢谢@Laert
      • 这确实是更好的方法!
      【解决方案7】:

      在您的按钮点击事件中添加以下代码:

      InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
      inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
      

      【讨论】:

      • 像魅力一样工作!
      【解决方案8】:

      这个解决方案非常适合我:

      private void showKeyboard(EditText editText) {
          editText.requestFocus();
          editText.setFocusableInTouchMode(true);
          InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.showSoftInput(editText, InputMethodManager.RESULT_UNCHANGED_SHOWN);
          editText.setSelection(editText.getText().length());
      }
      
      private void closeKeyboard() {
          InputMethodManager inputManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
          inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
      }
      

      【讨论】:

        【解决方案9】:

        试试这个...

        1. 用于显示键盘

          editText.requestFocus();
          InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
          imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
          
        2. 隐藏键盘

          InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); 
          inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
          

        【讨论】:

        • "For Hide keyboard" 方法是一个工具(如果可见则隐藏,如果隐藏则显示)不是隐藏,
        【解决方案10】:
        View view = this.getCurrentFocus();
        if (view != null) {
        InputMethodManager imm =(InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);enter code here}
        

        【讨论】:

        • 嗨!虽然这个答案可能足以满足 @Andrew 的需要,但如果您可以通过一些解释对其进行扩展,以确保未来的读者可以充分受益,那就太好了!
        【解决方案11】:

        Kotlin 示例:

        val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        

        来自片段:

        inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
        

        来自活动:

        inputMethodManager.hideSoftInputFromWindow(currentFocus?.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
        

        【讨论】:

          【解决方案12】:

          您在按钮单击事件中使用此代码

          // Check if no view has focus:
          View view = this.getCurrentFocus();
          if (view != null) {  
              InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
          }
          

          【讨论】:

            【解决方案13】:

            崩溃空点异常修复: 我遇到过当用户单击按钮时键盘可能无法打开的情况。您必须编写一个 if 语句来检查 getCurrentFocus() 是否为空:

                        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if(getCurrentFocus() != null) {
                        inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            

            【讨论】:

              【解决方案14】:

              如果你设置android:singleLine="true",按钮自动隐藏键盘…

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2010-12-31
                • 1970-01-01
                • 1970-01-01
                • 2012-07-21
                • 1970-01-01
                • 2015-02-27
                • 2015-08-06
                • 1970-01-01
                相关资源
                最近更新 更多