2016-12-20 16 views
2

このケースでは、ソフトキーボードがオンであることを確認して、コードを閉じることができません。次のコードを使用すると、コードがフォーカスを見つけることができないため、キーボードは閉じられませんが、キーボードはオンですだから私はそれをどのように隠すことができますか?Androidでフォーカスのないソフトキーボードを隠すには?

private void hideSoftKeyboard() { 
    Activity activity = (Activity) sContext; 
    View view = activity.getCurrentFocus(); 
    if (view != null) { 

     InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0); 
     //((Activity) sContext).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
    } else { 

      Log.i(sClassTag,"focus not found"); 

    } 
} 
+0

'ビュービューを見つけることができます= this.getCurrentFocus(); if(view!= null){ InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken()、0); } ' –

+0

' InputMethodManager imm =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY、0); ' –

+0

[Androidソフトキーボードを閉じる/隠す]の複製があります(http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard)。 ) –

答えて

3

あなたの集中ビューを含むウィンドウのトークンを渡し、hideSoftInputFromWindowを呼び出し、InputMethodManagerを使用して仮想キーボードを非表示にするアンドロイドを強制することができ、この

を使用してみてください。

View view = this.getCurrentFocus(); 
if (view != null) { 
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 
} 

これにより、すべての状況でキーボードが非表示になります。場合によっては、ユーザが明示的に強制的に表示されないように(メニューを押しながら)キーボードを非表示にするために、2番目のパラメータとしてInputMethodManager.HIDE_IMPLICIT_ONLYを渡したい場合があります。

またはこの

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); 

あなたが詳細here

関連する問題