Answers:
您要禁用还是关闭虚拟键盘?
如果您只想关闭它,则可以在按钮的单击事件中使用以下代码行
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
getActivity())getCurrentFocus().getWindowToken()用于第一个arg hideSoftInputFromWindow()。另外,如果要在更改活动时尝试使其消失,请在onPause()而不是在onStop()中进行操作。
上面的解决方案不适用于所有设备,而且它使用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);
}
}
isAcceptingText()使这个答案比其他人更好
这是我的解决方案
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类中)
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()
使用InputMethodManager的第一个解决方案对我来说就像冠军,getWindow()。setSoftInputMode方法不在android 4.0.3 HTC Amaze上。
@Ethan Allen,我不需要将编辑文本定为最终文本。也许您正在使用声明了包含方法的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);
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();
}
}
此解决方案确保它隐藏了键盘(如果未打开)也不会执行任何操作。它使用扩展名,因此可以在任何上下文所有者类中使用它。
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)
}
}
通过使用视图的上下文,我们可以使用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()