点击按钮后,关闭键盘(休息,如果没有的EditText专注于) [英] Closing keyboard after button click (breaks if no EditText is focused on)
问题描述
我把这个在我的按钮监听器:
InputMethodManager inputManager =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
这完全适用于键盘处于起来:它会关闭键盘,然后继续执行。问题是,当没有的EditText
是有史以来pressed(他们没有被突出显示/集中),它打破和应用程序停止工作。
我想这将是很好,如果我可以检查是否一个EditText曾经去过pressed。
我试图做这个检查:
InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);如果(imm.isActive()),如果任何视图目前在输入法主动//返回true
{
imm.hideSoftInputFromWindow(getCurrentFocus()getWindowToken(),InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
修改
我落得这样做的:
我创造了这个方法:
公共静态无效hideSoftKeyboard(活动活动,查看视图)
{
InputMethodManager IMM =(InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(),0);
}
然后我叫法
在我的按钮侦听是这样的:
hideSoftKeyboard(MainActivity.this,V); // MainActivity是我的类和V的名字是我在按钮监听器方法使用的视图。
您可以使用此:
您可以做一个方法,并调用它的onCreate()方法:
公共静态无效hideSoftKeyboard(活动活动,查看视图){
InputMethodManager IMM =(InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(),0);}
或者干脆你可以在manifest文件这样的添加:
<活动机器人:名字=com.your.package.ActivityName
机器人:windowSoftInputMode =stateHidden/>
I put this on my button listener:
InputMethodManager inputManager =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
It works perfectly for when the keyboard is up: it closes the keyboard and then continues its execution. The problem is that when no EditText
was ever pressed (none of them are highlighted/focused on), it breaks and the app "stops working".
I guess it would be nice if I could check if an EditText had ever been pressed.
I tried to do this to check:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive()) // returns true if any view is currently active in the input method
{
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);
}
EDIT
I ended up doing this:
I created this method:
public static void hideSoftKeyboard (Activity activity, View view)
{
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
I then called the method
in my button listener like this:
hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of my class and v is the View I used in my button listener method.
you can use this:
You can make a method and call it on onCreate() method:
public static void hideSoftKeyboard (Activity activity, View view) {
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);}
or simply you can add in manifest file like this:
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />
这篇关于点击按钮后,关闭键盘(休息,如果没有的EditText专注于)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!