35

ボタンをクリックした後、Androidキーボードを非表示にする必要があります。

これを行う方法の例をたくさん見てきましたが、それらはすべて特定のeditTextオブジェクトを使用しているように見えます。

例えば

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

私の問題は、画面を動的に構築しているため、テキストフィールドを編集するためにたてがみがある可能性があることです。非表示にするeditTextオブジェクトを指定せずに、キーボードを非表示にする方法はありますか。

4

9 に答える 9

58

代わりに、レイアウトに設定することもできます。

LinearLayout mainLayout;

// Get your layout set up, this is just an example
mainLayout = (LinearLayout)findViewById(R.id.myLinearLayout);

// Then just use the following:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainLayout.getWindowToken(), 0);

EditTextこれは、配置されているオブジェクト(または他のオブジェクト)の数に関係なく、レイアウトが作成されると想定した例です。

編集:また、私が非常に役立つと思うのは、アクティビティが最初に起動したときにキーボードが非表示になっていることを確認することです(つまり、EditTextが最初に焦点を当てている場合)。onCreate()それを行うために、私はこれをアクティビティのメソッドに入れました:

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
于 2012-11-27T21:17:25.223 に答える
56

キーボードが開いていない場合や、キーボードを使用している場合はコードを隠すアプリがクラッシュするため、trycatchblogを使用することを忘れないでください

try {
    InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
    // TODO: handle exception
}
于 2014-12-01T12:36:47.667 に答える
14

次のコードを使用して、おそらくボタンクリックイベントでキーボードを非表示にできます。

//================ Hide Virtual Key Board When  Clicking==================//

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);

//======== Hide Virtual Keyboard =====================//
于 2015-07-24T08:25:41.017 に答える
9

問題がアクティビティにある場合は、次のように機能します。

    try {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {
        // TODO: handle exception
    }

それ以外の場合、コードがフラグメントで必要な場合は、次のようにします

    try {
        InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {
        // TODO: handle exception
    }

これは、ボタンクリックまたはイベントブロック内に書き込まれたときに特定と見なされるその他のイベントでのキーボードの非表示を処理します。

于 2017-02-10T09:29:25.760 に答える
4
edittext.onEditorAction(EditorInfo.IME_ACTION_DONE);
于 2017-11-09T09:53:48.323 に答える
4

記録のために、@burmatと@PrashantMaheshwariAndroの回答に基づいています

次のようにクリックボタンを呼び出したとしましょう。ここで、buttonAndroidLogin_buttonはButtonオブジェクトです。

protected void onCreate(Bundle savedInstanceState) {
    // your code...
    buttonAndroidLogin_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            hideKeyboard((Button)v);
            // ....
} // end onCreate

アクティビティでは、次のメソッドを設定する必要があります

public void hideKeyboard(View view) {
    try {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    } catch(Exception ignored) {
    }
}

同じボタンを使用して入力を非表示にするため、線形レイアウト、テキストビュー、またはその他の任意のコントロールは必要ありません。また、コードはより多くのボタンに再利用できます。

于 2019-02-07T22:36:58.853 に答える
3

このコードを使用します

// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
于 2016-10-26T05:44:32.037 に答える
3

KOTLINで:

あなたのfragment

次のような拡張機能を作成します。

fun Fragment.hideKeyboard() {
    val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}

次に、次のように使用します。

hideKeyboard()

あなたのactivity

次のような拡張機能を作成します。

fun AppCompatActivity.hideKeyboard() {
    val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}

次に、次のように使用します。

   hideKeyboard()
于 2020-09-23T06:26:31.717 に答える
0
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
于 2017-04-11T17:08:32.427 に答える