Androidはキーボードを終了します


147

ボタンが押されたときにキーボードを閉じるにはどうすればよいですか?


EditTextをFocusable = Falseにしてください。完全に無効にしますか?
st0le 2010

回答:


325

仮想キーボードを無効にするか、閉じますか?

単に閉じたい場合は、ボタンのクリックイベントで次のコード行を使用できます。

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

1
これに関する2つの問題.... 1つは、myEditTextがFinalである必要があることです。2つ目は、どのEditTextボックスにフォーカスがあるかを知る必要があることです。これに対する解決策はありますか?
イーサンアレン

78
ここでつまずく誰のために、あなたは活動の(あなたがしているアクティビティまたはフラグメントのいずれかを使用することができますgetActivity()getCurrentFocus().getWindowToken()への最初の引数のためにhideSoftInputFromWindow()。また、アクティビティを変更するときにそれをなくそうとしているのではonPause()なく、でonStop()実行してください。
Drake

2
この回答とここでのコメントを組み合わせると、私の問題は完全に解決されました!
aveschini 2013年

9
キーボードを却下するための、醜い、醜いアプローチ。将来、このような単純なことを行うためのよりクリーンな方法があることを願っています。
Subby

73

上記の解決策はすべてのデバイスで機能するわけではなく、さらにパラメーターとして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);
    }
}

3
isAcceptingText()この回答を他の回答よりも優れたものにした
user1506104 '25 / 04/25

良い解決策...まれに、キーボードが画面上にあるときにisAcceptingText()がfalseを返すことがあります。
ジョージー

29

これは私の解決策です

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);
    }
}

それは、キーボードを表示する原因となったビューを知っている場合です。
RPM

16

ボタンクリックイベントでこのコードを使用することもできます

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

13

これは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()

5

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);

3
Stack Overflowへようこそ!これは実際にはコメントであり、回答ではありません。もう少し担当者がいると、コメントを投稿できるようになります
ジャック、

4
    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();
    }
}

1

このソリューションは、キーボードが非表示になっている場合でも、キーボードが非表示であることを確認します。拡張機能を使用するため、任意のコンテキスト所有者クラスから使用できます。


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)
    }
}


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()
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.