Android 安卓键盘
按下按钮时如何关闭键盘?要禁用或关闭虚拟键盘吗 如果您想取消它,您可以在按钮的点击事件中使用以下代码行Android 安卓键盘,android,Android,按下按钮时如何关闭键盘?要禁用或关闭虚拟键盘吗 如果您想取消它,您可以在按钮的点击事件中使用以下代码行 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 您还可以在按钮单击事件中使用此代码 getWindow().setSoftInputM
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 HTC Amaze上没有 @伊森·艾伦,我不需要让编辑文本成为最终文本。也许您正在使用一个声明了包含方法的EditText内部类?您可以将EditText作为活动的类变量。或者在内部类/方法中声明一个新的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);
}
}
这是一个Kotlin解决方案(在线程中混合各种答案)
创建扩展函数(可能在公共ViewHelpers类中)
然后简单地使用:
// 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)
}
}
使EditText可聚焦=False执行此任务。你想完全禁用它吗?这有两个问题。。。。一是myEditText需要是最终版本。第二,我必须知道哪个文本框有焦点。有什么解决方案吗?对于在这里遇到问题的任何其他人,您可以使用活动的(您所在的活动或片段
getActivity()
)getCurrentFocus().getWindowToken()
作为hideOffInputFromWindow()的第一个参数。另外,如果你想让它在改变活动时消失,请在onPause()
而不是onStop()
中进行。这个答案,再加上上面的评论,完全解决了我的问题!这是一种多么丑陋、丑陋的方法来抛弃键盘。我希望将来有一种更干净的方法来做这样一件简单的事情。欢迎使用Stack Overflow!这实际上是一个评论,而不是一个答案。再重复一点,.isAcceptingText()
使这个答案比其他好的解决方案更好。。。在极少数情况下,当键盘仍在屏幕上时,isAcceptingText()会返回false,例如,从EditText单击可选择但不能在同一窗口中编辑的文本。如果您知道导致键盘显示的视图,则返回false。
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)
}
}