回答:
仮想キーボードを無効にするか、閉じますか?
単に閉じたい場合は、ボタンのクリックイベントで次のコード行を使用できます。
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
getActivity())getCurrentFocus().getWindowToken()への最初の引数のためにhideSoftInputFromWindow()。また、アクティビティを変更するときにそれをなくそうとしているのではonPause()なく、でonStop()実行してください。
上記の解決策はすべてのデバイスで機能するわけではなく、さらにパラメーターとしてEditTextを使用しています。これは私の解決策です、この単純なメソッドを呼び出すだけです:
private void hideSoftKeyBoard() {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if(imm.isAcceptingText()) { // verify if the soft keyboard is open
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
isAcceptingText()この回答を他の回答よりも優れたものにした
これは私の解決策です
public static void hideKeyboard(Activity activity) {
View v = activity.getWindow().getCurrentFocus();
if (v != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
これはKotlinソリューションです(スレッドでさまざまな答えを混ぜる)
拡張関数を作成します(おそらく共通のViewHelpersクラスにあります)。
fun Activity.dismissKeyboard() {
val inputMethodManager = getSystemService( Context.INPUT_METHOD_SERVICE ) as InputMethodManager
if( inputMethodManager.isAcceptingText )
inputMethodManager.hideSoftInputFromWindow( this.currentFocus.windowToken, /*flags:*/ 0)
}
次に、単に使用して消費します:
// from activity
this.dismissKeyboard()
// from fragment
activity.dismissKeyboard()
InputMethodManagerを使用した最初のソリューションは私にとっては大胆に機能しましたが、getWindow()。setSoftInputModeメソッドはandroid 4.0.3 HTC Amazeでは機能しませんでした。
@イーサン・アレン、エディットテキストをファイナルにする必要はありませんでした。おそらく、含むメソッドを宣言したEditText内部クラスを使用していますか?EditTextをアクティビティのクラス変数にすることができます。または、内部クラス/メソッド内で新しいEditTextを宣言し、findViewById()を再度使用します。また、フォームのどのEditTextにフォーカスがあるかを知る必要があることもわかりませんでした。私は任意に1つを選択して使用できます。そのようです:
EditText myEditText= (EditText) findViewById(R.id.anyEditTextInForm);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
public static void hideSoftInput(Activity activity) {
try {
if (activity == null || activity.isFinishing()) return;
Window window = activity.getWindow();
if (window == null) return;
View view = window.getCurrentFocus();
//give decorView a chance
if (view == null) view = window.getDecorView();
if (view == null) return;
InputMethodManager imm = (InputMethodManager) activity.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm == null || !imm.isActive()) return;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
} catch (Throwable e) {
e.printStackTrace();
}
}
このソリューションは、キーボードが非表示になっている場合でも、キーボードが非表示であることを確認します。拡張機能を使用するため、任意のコンテキスト所有者クラスから使用できます。
fun Context.dismissKeyboard() {
val imm by lazy { this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager }
val windowHeightMethod = InputMethodManager::class.java.getMethod("getInputMethodWindowVisibleHeight")
val height = windowHeightMethod.invoke(imm) as Int
if (height > 0) {
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}
}
ビューのコンテキストを使用することにより、Kotlinの次の拡張メソッドで目的の結果を得ることができます。
/**
* Get the [InputMethodManager] using some [Context].
*/
fun Context.getInputMethodManager(): InputMethodManager {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return getSystemService(InputMethodManager::class.java)
}
return getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
}
/**
* Dismiss soft input (keyboard) from the window using a [View] context.
*/
fun View.dismissKeyboard() = context
.getInputMethodManager()
.hideSoftInputFromWindow(
windowToken
, 0
)
これらが準備できたら、次を呼び出してください:
editTextFoo.dismiss()