35

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

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

例如

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

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

4

9 回答 9

58

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

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对象(或其他对象)都将创建您的布局的示例。

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

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
于 2012-11-27T21:17:25.223 回答
56

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

try {
    InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
    // TODO: handle exception
}
于 2014-12-01T12:36:47.667 回答
14

您可以使用以下代码隐藏键盘,可能在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 =====================//
于 2015-07-24T08:25:41.017 回答
9

如果问题出在一项活动上,那么以下操作将起作用:

    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
    }

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

于 2017-02-10T09:29:25.760 回答
4
edittext.onEditorAction(EditorInfo.IME_ACTION_DONE);
于 2017-11-09T09:53:48.323 回答
4

记录在案并基于@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

在活动上,您必须设置下一个方法

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

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

于 2019-02-07T22:36:58.853 回答
3

您使用此代码

// 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);
}
于 2016-10-26T05:44:32.037 回答
3

在科特林:

在你的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()
于 2020-09-23T06:26:31.717 回答
0
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
于 2017-04-11T17:08:32.427 回答