我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
当前回答
试试这个强制完全android软输入键盘
在Helper类中创建方法。
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null)
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
其他回答
对于Xamarin.Android:
public void HideKeyboard()
{
var imm = activity.GetSystemService(Context.InputMethodService).JavaCast<InputMethodManager>();
var view = activity.CurrentFocus ?? new View(activity);
imm.HideSoftInputFromWindow(view.WindowToken, HideSoftInputFlags.None);
}
如果有人感兴趣的话,我已经为Kotlin写了一个小扩展,但没有太多测试:
fun Fragment.hideKeyboard(context: Context = App.instance) {
val windowToken = view?.rootView?.windowToken
windowToken?.let {
val imm = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
}
App.instance是存储在Application中的静态“this”Application对象
更新:在某些情况下,windowToken为空。我增加了使用反射来检测键盘是否关闭的额外关闭方式
/**
* If no window token is found, keyboard is checked using reflection to know if keyboard visibility toggle is needed
*
* @param useReflection - whether to use reflection in case of no window token or not
*/
fun Fragment.hideKeyboard(context: Context = MainApp.instance, useReflection: Boolean = true) {
val windowToken = view?.rootView?.windowToken
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
windowToken?.let {
imm.hideSoftInputFromWindow(windowToken, 0)
} ?: run {
if (useReflection) {
try {
if (getKeyboardHeight(imm) > 0) {
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS)
}
} catch (exception: Exception) {
Timber.e(exception)
}
}
}
}
fun getKeyboardHeight(imm: InputMethodManager): Int = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight").invoke(imm) as Int
public final class UtilsKeyboard {
/**
* Sets the cursor at the end of this edit text and shows a keyboard
*/
public static void focusKeyboard(@NonNull EditText editText) {
editText.requestFocus();
editText.setSelection(editText.getText().length());
showKeyboard(editText);
}
private static void showKeyboard(@NonNull View view) {
final InputMethodManager manager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (manager != null) {
manager.showSoftInput(view, InputMethodManager.SHOW_FORCED);
}
}
public static boolean hideKeyboard(@NonNull Window window) {
View view = window.getCurrentFocus();
return hideKeyboard(window, view);
}
private static boolean hideKeyboard(@NonNull Window window, @Nullable View view) {
if (view == null) {
return false;
}
InputMethodManager inputMethodManager = (InputMethodManager) window.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
return inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
return false;
}
public static boolean hideKeyboard(@Nullable View view) {
if (view == null) {
return false;
}
InputMethodManager inputMethodManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
return inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
return false;
}
/**
* This will hide the keyboard and unfocus any focused view.
*/
public static void unfocusKeyboard(@NonNull Window window) {
// When showing the bottom menu, make sure the keyboard isn't and that no view has focus
hideKeyboard(window);
final View focused = window.getCurrentFocus();
if (focused != null) focused.clearFocus();
}}
这对我很有用
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive())
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
在Android中,要通过InputMethodManage隐藏Vkeyboard,您可以通过传递包含焦点视图的窗口的标记来隐藏SoftInputFromWindow。
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager im =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
通过调用editText.clearFocus(),然后调用InputMethodManager.HIDE_IMPLICIT_ONLY甚至可以工作