2013-08-24 40 views
-3

我把這個在我的按鈕偵聽:關閉鍵盤按鈕,點擊(休息,如果沒有的EditText專注於)

InputMethodManager inputManager =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 

inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 

它完全適用於鍵盤時達:它關閉鍵盤,然後繼續執行。問題是,當沒有EditText曾經按下(他們沒有突出顯示/重點),它打破了,應用程序「停止工作」。

我想如果我能檢查EditText是否曾經被按下過,那將會很好。

我試圖做此檢查:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 

if (imm.isActive()) // returns true if any view is currently active in the input method 
{ 
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN); 
} 

編輯

我落得這樣做:

我創造了這個方法:

public static void hideSoftKeyboard (Activity activity, View view) 
{ 
    InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0); 
} 

然後我在我的按鈕監聽中調用了method NER這樣的:

hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of my class and v is the View I used in my button listener method. 
+0

你檢查了logCat,它顯示了什麼錯誤? –

+0

@NiteshVerma我不熟悉logCat。我確實找到了一個平庸的解決方案,但是,我會發布。 –

+0

使用豐聯 關閉/隱藏 http://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard –

回答

1

您可以使用此:

,您可以撥打方法並調用它的onCreate()方法:

public static void hideSoftKeyboard (Activity activity, View view) { 
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);} 

或者乾脆你可以在清單文件中這樣補充:

<activity android:name="com.your.package.ActivityName" 
    android:windowSoftInputMode="stateHidden" /> 
+0

我已經嘗試了很多解決方案**非常類似於這個,所以我甚至會嘗試它,但我很高興我做到了,因爲它的工作完美。這是完全一般的,不管做什麼都行:你不必創建任何特定的實例。 –

+0

哦...好的.... !!! – Piyush

0

平庸/壞的解決方案,因爲我希望它不會爲一般的工作:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 

EditText x = (EditText)findViewById(R.id.editText); 
x.requestFocus(); // this way no matter what, an EditText is focused on. 

imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);