これをボタンリスナーに置きます:
InputMethodManager inputManager =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
キーボードがアップしているときに完全に機能します。キーボードを閉じてから実行を続けます。問題は、何EditText
も押されていない場合 (どれも強調表示/フォーカスされていない場合)、アプリが壊れてアプリが「動作を停止」することです。
EditText が押されたことがあるかどうかを確認できればいいと思います。
私は確認するためにこれをやろうとしました:
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);
}
編集
私はこれをやってしまった:
このメソッドを作成しました:
public static void hideSoftKeyboard (Activity activity, View view)
{
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
次にmethod
、ボタンリスナーで次のように呼び出しました。
hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of my class and v is the View I used in my button listener method.