我的布局中有一个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);
其他回答
在阅读了上面和另一篇文章中的所有答案后,我仍然没有成功地让键盘自动打开。
在我的项目中,我动态地创建了一个对话框(AlertDialog)(通过不使用或使用最少的XML对其进行编程)。
所以我在做一些事情,比如:
dialogBuilder = new AlertDialog.Builder(activity);
if(dialogBuilder==null)
return false; //error
inflater = activity.getLayoutInflater();
dialogView = inflater.inflate(layout, null);
...
在完成所有视图(TextView、ImageView、EditText等)的设置后,我做到了:
alertDialog = dialogBuilder.create();
alertDialog.show();
在玩转了所有答案后,我发现如果你知道把请求放在哪里,大多数答案都有效。。。这是一切的关键。
所以,诀窍是在创建对话框之前使用它:alertDialog.show()。在我的例子中,这就像魅力一样:
alertDialog = dialogBuilder.create();
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//And only when everything is finished - let's bring up the window -
alertDialog.show();
//Viola... keyboard is waiting for you open and ready...
//Just don't forget to request focus for the needed view (i.e. EditText..)
我很确定这一原则在所有窗口中都是一样的,所以请注意“showKeyboard”代码的位置——它应该在窗口启动之前。
来自Android SDK开发团队的一个小请求:
我认为所有这些都是不必要的,因为你可以看到来自世界各地的数千名程序员正在处理这个荒谬而琐碎的问题,而其解决方案应该是干净而简单的:IMHO如果我将requestFocus()获取到面向输入的视图(如EditText),键盘应该自动打开,除非用户要求不打开,因此,我认为requestFocus方法是这里的关键,应该接受默认值为true的布尔showSoftKeyboard:view.requestFocus(布尔showSoftKeyboard);
希望这能帮助像我这样的人。
非常简单的方法
我在所有的项目中都这样做,并且像做梦一样工作。在声明layout.xml中,只需添加以下一行:
android:focusableInTouchMode="true"
完整代码示例:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</android.support.constraint.ConstraintLayout>
private void hideSoftKeyboard() {
View view = getView();
if (view != null) {
InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
在某些情况下,除了所有其他方法外,这种方法都可以工作。这节省了我的一天:)
public static void hideSoftKeyboard(Activity activity) {
if (activity != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
if (activity.getCurrentFocus() != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
inputManager.hideSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
}
}
}
public static void hideSoftKeyboard(View view) {
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null) {
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
}
Kotlin版本
val imm: InputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
//Hide:
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
//Show
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);