我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
我的布局中有一个EditText和一个Button。
在编辑字段中写入并单击按钮后,我想在触摸键盘外部时隐藏虚拟键盘。我假设这是一段简单的代码,但我在哪里可以找到它的示例?
当前回答
有时你只需要按回车键来折叠键盘在“编辑文本”框中指定属性
android:imeOptions="actionDone"
这将将Enter按钮更改为Done按钮,该按钮将关闭键盘。
其他回答
对于kotlin用户,这里有一个kotlin扩展方法,它适用于我的用例:
fun View.hideKeyboard() {
val imm = this.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
将其放在名为ViewExtensions的文件中(或其他文件),并像普通方法一样在视图中调用它。
您可以使用InputMethodManager强制Android隐藏虚拟键盘,调用hideSoftInputFromWindow,传入包含焦点视图的窗口的标记。
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
这将在所有情况下强制隐藏键盘。在某些情况下,您需要将InputMethodManager.HIDE_IMPLICIT_ONLY作为第二个参数传递,以确保仅在用户未显式强制显示键盘时(通过按住菜单)隐藏键盘。
注意:如果您想在Kotlin中执行此操作,请使用:上下文getSystemService(Context.INPUT_METHOD_SERVICE)作为InputMethodManager
Kotlin语法
// Only runs if there is a view that is currently focused
this.currentFocus?.let { view ->
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
imm?.hideSoftInputFromWindow(view.windowToken, 0)
}
在绝望中尝试了所有的方法,结合了所有方法,当然,在Android 4.0.3中键盘不会关闭(它在Honeicomb AFAIR中确实有效)。
然后我突然发现了一个明显获胜的组合:
textField.setRawInputType(InputType.TYPE_CLASS_TEXT |InputType.TYPE_TEXT_VARIATION_NORMAL);
结合你平时的食谱
blahblaj.hideSoftInputFromWindow ...
希望这能阻止某人自杀。。我很接近它。当然,我不知道为什么它会起作用。
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();
}}
对于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);
}