138

、ボタン、および がありActivityます。目的は、 に検索画面を入力し、ボタンを押して、検索結果をこのリストに入力することです。EditTextListViewEditText

これはすべて完全に機能していますが、仮想キーボードの動作がおかしいです。

をクリックするEditTextと、仮想キーボードが表示されます。仮想キーボードの「完了」ボタンをクリックすると、消えます。ただし、仮想キーボードで [完了] をクリックする前に検索ボタンをクリックすると、仮想キーボードが残り、削除できません。[完了] ボタンをクリックしても、キーボードは閉じません。「完了」ボタンが「完了」から矢印に変わり、表示されたままになります。

ご協力いただきありがとうございます

4

14 に答える 14

60
mMyTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            // hide virtual keyboard
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(m_txtSearchText.getWindowToken(), 
                                      InputMethodManager.RESULT_UNCHANGED_SHOWN);
            return true;
        }
        return false;
    }
});
于 2010-09-17T15:47:35.287 に答える
32

以下のコードを使用

your_button_id.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        try  {
            InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {

        }
    }
});
于 2014-12-01T12:43:31.107 に答える
13

OnEditorActionListenerEditViewに実装する必要があります

public void performClickOnDone(EditView editView, final View button){
    textView.setOnEditorActionListener(new OnEditorActionListener() {

        @Override
        public boolean onEditorAction(EditView v, int actionId, KeyEvent event) {
            hideKeyboard();
            button.requestFocus();
            button.performClick();
            return true;
        }
    });

そして、あなたはキーボードを隠す:

public void hideKeybord(View view) {
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),
                                  InputMethodManager.RESULT_UNCHANGED_SHOWN);
}

また、を使用してボタンに隠れているキーボードを起動する必要がありますonClickListener

仮想キーボードで[完了]をクリックすると、ボタンも同じように動作します。キーボードを非表示にして、クリックアクションを実行します。

于 2010-08-03T20:45:02.827 に答える
11

ボタン クリック イベント内に次のコードを追加します。

InputMethodManager inputManager = (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
于 2015-04-09T10:06:34.007 に答える
4

これを試して...

  1. 表示用キーボード

    editText.requestFocus();
    InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    
  2. キーボードを非表示にする

    InputMethodManager inputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); 
    inputManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
    
于 2016-11-09T09:39:50.760 に答える
0

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

// 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:49:43.883 に答える
0

クラッシュ Null Point Exception の修正: ユーザーがボタンをクリックしたときにキーボードが開かない場合がありました。getCurrentFocus() が null でないことを確認するには、if ステートメントを記述する必要があります。

            InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if(getCurrentFocus() != null) {
            inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
于 2017-04-29T23:29:27.557 に答える
-2

を設定するandroid:singleLine="true"と、自動的にボタンがキーボードを隠します。

于 2016-04-14T09:16:51.167 に答える