【问题标题】:Android:Hide keyboard after button clickAndroid:单击按钮后隐藏键盘
【发布时间】:2012-11-15 14:17:40
【问题描述】:

单击按钮后,我需要隐藏 android 键盘。

我已经看到了很多如何做到这一点的示例,但是,它们似乎都使用了特定的 editText 对象。

例如

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

我的问题是我正在动态构建屏幕,因此可能会有鬃毛编辑文本字段。有没有一种方法可以隐藏键盘,而无需我指定要隐藏它的 editText 对象。

【问题讨论】:

标签: java android keyboard hide


【解决方案1】:

在科特林:

在你的fragment

像这样创建扩展:

fun Fragment.hideKeyboard() {
    val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}

然后像这样使用它:

hideKeyboard()

在你的activity

像这样创建扩展:

fun AppCompatActivity.hideKeyboard() {
    val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}

然后像这样使用它:

   hideKeyboard()

【讨论】:

    【解决方案2】:

    记录并基于@burmat 和@Prashant Maheshwari Andro 的回答

    假设您按如下方式调用单击按钮。其中 buttonAndroidLogin_button 是 Button 对象。

    protected void onCreate(Bundle savedInstanceState) {
        // your code...
        buttonAndroidLogin_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideKeyboard((Button)v);
                // ....
    } // end onCreate
    

    在activity上,必须设置next方法

    public void hideKeyboard(View view) {
        try {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        } catch(Exception ignored) {
        }
    }
    

    它使用相同的按钮隐藏输入,因此我们不需要任何线性布局、文本视图或任何其他任意控件。此外,代码可重复用于更多按钮。

    【讨论】:

      【解决方案3】:

      您可以使用以下代码隐藏键盘,可能在 Button click Event 上:

      //================ Hide Virtual Key Board When  Clicking==================//
      
      InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
      imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
      
      //======== Hide Virtual Keyboard =====================//
      

      【讨论】:

        【解决方案4】:
        edittext.onEditorAction(EditorInfo.IME_ACTION_DONE);
        

        【讨论】:

        • 被低估的答案
        【解决方案5】:
        InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
        

        【讨论】:

          【解决方案6】:

          如果问题出在某个活动上,只需以下方法即可:

              try {
                  InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
              } catch (Exception e) {
                  // TODO: handle exception
              }
          

          如果片段中需要代码,则执行以下操作

              try {
                  InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
              } catch (Exception e) {
                  // TODO: handle exception
              }
          

          这将处理在按钮单击或任何其他在事件块中写入时被视为特定事件时隐藏的键盘。

          【讨论】:

            【解决方案7】:

            您使用此代码

            // 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);
            }
            

            【讨论】:

              【解决方案8】:

              您可以改为将其设置为您的布局,即:

              LinearLayout mainLayout;
              
              // Get your layout set up, this is just an example
              mainLayout = (LinearLayout)findViewById(R.id.myLinearLayout);
              
              // Then just use the following:
              InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(mainLayout.getWindowToken(), 0);
              

              这是一个示例,假设无论放置多少 EditText 对象(或其他对象)都将创建您的布局。

              编辑:另外,我发现非常有用的是确保在活动首次启动时隐藏键盘(即:如果EditText 是第一个关注的对象)。为此,我将其放入 Activity 的onCreate() 方法中:

               this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
              

              【讨论】:

              • 请注意,它也适用于其他视图部分,例如一个 EditText :) 然后您将更改以下行:imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
              【解决方案9】:

              不要忘记使用 try catch 博客,因为如果您的键盘未打开并且如果您使用键盘隐藏代码应用程序将崩溃

              try {
                  InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
              } catch (Exception e) {
                  // TODO: handle exception
              }
              

              【讨论】:

              • 迄今为止最简单优雅的解决方案
              • 最优雅、通用和简单的解决方案,我喜欢它
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2017-03-08
              • 2017-05-23
              • 2021-03-23
              • 1970-01-01
              • 1970-01-01
              • 2017-01-31
              • 2011-07-30
              相关资源
              最近更新 更多