安卓:点击按钮后隐藏键盘

38

我需要在按钮点击后隐藏Android键盘。

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

例如:

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

我的问题是我正在动态构建屏幕,因此可能有很多编辑文本字段。是否有一种方法可以隐藏键盘,而不必指定我要为哪个editText对象隐藏它。

我的问题是我正在动态构建屏幕,因此可能存在多个编辑文本字段。是否有一种方式可以在不必指定要隐藏哪个editText对象的情况下隐藏键盘。

你可以在整个Activity中隐藏它,这很好地概括了它: https://dev59.com/rWsz5IYBdhLWcg3wlY-9 - A--C
10个回答

63

你可以将它设置为你的布局,例如:

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

4
请注意,它对其他视图部件也有效,例如EditText :) 然后您需要更改以下行:imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); - Michael

57

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

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

3
迄今为止最简单优雅的解决方案 - Kumar Saurabh
最优雅、通用和简单的解决方案,我喜欢它。 - Alex Escobar

15

您可以使用以下代码隐藏键盘,可能是在按钮单击事件上:

//================ 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 =====================//

9
edittext.onEditorAction(EditorInfo.IME_ACTION_DONE);

2
被低估的答案 - DennisVA

9
如果问题出现在一个activity中,只需按照以下步骤操作即可:
    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
    }

这将处理在按钮点击或任何其他特定事件中隐藏键盘的操作,当在事件块中编写时。

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) {
    }
}

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


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

3

在 Kotlin 中:

在你的 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()

对我来说没有用,而使用布局来获取windowToken则可以。 - tiktak
你说的windowToken是什么意思? - milad salimi

0
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(rootView.getWindowToken(), 0);

0
这段代码是用来在Fragment上移除屏幕上的键盘。 代码:
try {
  InputMethodManager inputMethodManager = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  inputMethodManager.hideSoftInputFromWindow(Objects.requireNonNull(requireActivity().getCurrentFocus()).getWindowToken(), 0);
} catch (Exception e) {
  Log.d("PROFILE KEYBOARD ERROR", e.toString());
}

你的回答可以通过提供更多的支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接