【发布时间】:2010-08-24 05:25:18
【问题描述】:
按下按钮时如何关闭键盘?
【问题讨论】:
-
使 EditText Focusable = False 完成这项工作。你想完全禁用它吗?
标签: android
按下按钮时如何关闭键盘?
【问题讨论】:
标签: android
您想禁用或关闭虚拟键盘吗?
如果您只想关闭它,可以在按钮的点击事件中使用以下代码行
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
【讨论】:
getActivity())getCurrentFocus().getWindowToken() 作为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);
}
}
【讨论】:
您也可以在按钮点击事件中使用此代码
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
【讨论】:
这是一个 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()
【讨论】: