2014-03-05 139 views
2

我需要隱藏軟鍵盤以響應點擊按鈕。 我看到了有關這一些帖子,我有嘗試過:Android:隱藏軟輸入鍵盤

InputMethodManager im = (InputMethodManager) getSystemService(
    Context.INPUT_METHOD_SERVICE); 
im.hideSoftInputFromWindow(myEditText1.getWindowToken(), 0); 

運作良好。但是現在我有兩個EditText視圖。現在我怎麼能隱藏軟鍵盤,無論EditText選擇了什麼?我試了一下還與

InputMethodManager im = (InputMethodManager) getSystemService(
    Context.INPUT_METHOD_SERVICE); 
im.hideSoftInputFromWindow(myEditText1.getWindowToken(), 0); 
im.hideSoftInputFromWindow(myEditText2.getWindowToken(), 0); 

,但沒有工作......

感謝您的幫助!

編輯: 找到解決辦法。在下面發佈。

回答

6

只要你不需要指向特定視圖。我使用這個和工作:)

InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0); 
+0

哦,很高興知道,謝謝! – marius

0
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 

你可以用參數「玩」來實現你想要的任何東西。 希望這有助於!

+0

不完全是我正在尋找,但謝謝你反正! :) – marius

2

一種解決方案是沒有得到來自的EditText窗口的道理,但是從布敦至極隱藏鍵盤itselfs:

InputMethodManager im = (InputMethodManager) getSystemService(
    Context.INPUT_METHOD_SERVICE); 
im.hideSoftInputFromWindow(hideKeyboardButton.getWindowToken(), 0);