如何以编程方式关闭/隐藏 Android 软键盘

如何以编程方式关闭/隐藏 Android 软键盘

技术背景

在 Android 开发中,有时需要以编程方式关闭或隐藏软键盘,例如在用户点击特定按钮、切换界面等场景下。然而,Android 对软键盘的处理 API 设计相对复杂,给开发者带来了一定的挑战。

实现步骤

Java 实现

1
2
3
4
5
6
// 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);
}

在某些情况下,可以将 InputMethodManager.HIDE_IMPLICIT_ONLY 作为第二个参数传入,确保仅在用户未明确强制显示键盘时隐藏它。

Kotlin 实现

1
2
3
4
5
// Only runs if there is a view that is currently focused
this.currentFocus?.let { view ->
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
imm?.hideSoftInputFromWindow(view.windowToken, 0)
}

静态工具方法(在 Activity 中使用)

1
2
3
4
5
6
7
8
9
10
public static void hideKeyboard(Activity activity) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
//Find the currently focused view, so we can grab the correct window token from it.
View view = activity.getCurrentFocus();
//If no view currently has focus, create a new one, just so we can grab a window token from it
if (view == null) {
view = new View(activity);
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

在 DialogFragment 中隐藏键盘

1
2
3
4
public static void hideKeyboardFrom(Context context, View view) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

使用新的 AndroidX Core 方法(1.5+)

1
2
3
4
5
fun View.hideKeyboard() = ViewCompat.getWindowInsetsController(this)
?.hide(WindowInsetsCompat.Type.ime())

fun Fragment.hideKeyboard() = ViewCompat.getWindowInsetsController(requireView())
?.hide(WindowInsetsCompat.Type.ime())

核心代码

完整的工具类示例(Java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class KeyBoard {

public static void show(Activity activity){
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); // show
}

public static void hide(Activity activity){
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide
}

public static void toggle(Activity activity){
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
if (imm.isActive()){
hide(activity);
} else {
show(activity);
}
}
}

使用示例

1
KeyBoard.toggle(activity);

最佳实践

  • 使用静态工具方法:将隐藏键盘的逻辑封装在静态工具方法中,方便在不同的 Activity 或 Fragment 中调用。
  • 处理无焦点视图的情况:当没有视图获得焦点时,可以创建一个新的视图来获取窗口令牌。
  • 使用新的 AndroidX Core 方法:对于 AndroidX Core 1.5+ 版本,使用 ViewCompat.getWindowInsetsController 方法可以更简洁地隐藏键盘。

常见问题

键盘在隐藏后重新出现

可能是因为在隐藏键盘后,用户又聚焦到了可输入的视图上。可以通过清除视图焦点来避免这种情况:

1
view.clearFocus();

在 DialogFragment 中无法隐藏键盘

使用 hideKeyboardFrom 方法,传入当前的 Context 和需要隐藏键盘的 View

不同 Android 版本兼容性问题

对于较旧的 Android 版本,可能需要使用反射或其他兼容性方法。例如,在 v3.2.4_r1 中,可以使用反射调用 setSoftInputShownOnFocus 方法:

1
2
3
4
5
6
7
8
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
try {
Method method = TextView.class.getMethod("setSoftInputShownOnFocus", boolean.class);
method.invoke(mEditText, false);
} catch (Exception e) {
// Fallback to the second method
}
}

如何以编程方式关闭/隐藏 Android 软键盘
https://119291.xyz/posts/2025-05-09.how-to-close-hide-android-soft-keyboard-programmatically/
作者
ww
发布于
2025年5月9日
许可协议