Android 取消键盘

当按下按钮时,我如何解除键盘?

73584 次浏览

要禁用或取消虚拟键盘吗?

如果你只是想解雇它,你可以使用下面的代码行在您的按钮的点击事件

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

也可以在按钮单击事件上使用此代码

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

使用 InputMethodManager 的第一个解决方案对我来说非常有用,getWindow ()。SetSoftInputMode 方法在 android 4.0.3上没有。

@ 伊桑 · 艾伦,我不需要把编辑文本写成最终版本。也许您正在使用声明包含方法的 EditText 内部类?您可以使 EditText 成为 Activity 的类变量。或者在内部类/方法中声明一个新的 EditText,然后再次使用 findViewById ()。而且,我也不需要知道表单中的哪个 EditText 具有焦点。我可以随便选一个然后用。像这样:

    EditText myEditText= (EditText) findViewById(R.id.anyEditTextInForm);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

上面的解决方案并不适用于所有设备,而且它使用 EditText 作为参数。这是我的解决方案,只要调用这个简单的方法:

private void hideSoftKeyBoard() {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);


if(imm.isAcceptingText()) { // verify if the soft keyboard is open
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}

这就是我的解决办法

public static void hideKeyboard(Activity activity) {
View v = activity.getWindow().getCurrentFocus();
if (v != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
    public static void hideSoftInput(Activity activity) {
try {
if (activity == null || activity.isFinishing()) return;
Window window = activity.getWindow();
if (window == null) return;
View view = window.getCurrentFocus();
//give decorView a chance
if (view == null) view = window.getDecorView();
if (view == null) return;


InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null || !imm.isActive()) return;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Throwable e) {
e.printStackTrace();
}
}

这里有一个 Kotlin 解决方案(把各种答案混合在一起)

创建扩展函数(可能在公共 ViewHelpers 类中)

fun Activity.dismissKeyboard() {
val inputMethodManager = getSystemService( Context.INPUT_METHOD_SERVICE ) as InputMethodManager
if( inputMethodManager.isAcceptingText )
inputMethodManager.hideSoftInputFromWindow( this.currentFocus.windowToken, /*flags:*/ 0)
}

然后简单地使用:

// from activity
this.dismissKeyboard()


// from fragment
activity.dismissKeyboard()

利用这个观点的背景,我们可以在 Kotlin 采用以下的扩展方法,达到预期的效果:

/**
* Get the [InputMethodManager] using some [Context].
*/
fun Context.getInputMethodManager(): InputMethodManager {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return getSystemService(InputMethodManager::class.java)
}


return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
}


/**
* Dismiss soft input (keyboard) from the window using a [View] context.
*/
fun View.dismissKeyboard() = context
.getInputMethodManager()
.hideSoftInputFromWindow(
windowToken
, 0
)

一旦这些准备就绪,只要打电话:

editTextFoo.dismiss()

此解决方案确保它隐藏键盘也不做任何事情,如果它没有打开。 它使用扩展,因此可以从任何上下文所有者类使用它。


fun Context.dismissKeyboard() {
val imm by lazy { this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }
val windowHeightMethod = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight")
val height = windowHeightMethod.invoke(imm) as Int
if (height > 0) {
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
}




科特林:

若要取消键盘,请在单击按钮时对相应的元素调用 clearFocus()

例如:

mSearchView.clearFocus()

在上述解决方案中进行了一些编辑并起作用

private void hideSoftKeyBoard() {
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(INPUT_METHOD_SERVICE);


if (imm.isAcceptingText()) { // verify if the soft keyboard is open
imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}
}