2011-10-17 37 views
24

現在,我試圖隱藏softkeyboard當用戶觸摸鍵盤外:如何獲取Activity的windowToken無視圖?

((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE)) 
.hideSoftInputFromWindow(editView.getWindowToken(), 
InputMethodManager.HIDE_NOT_ALWAYS); 

我希望把邏輯在我的基類的活動,所以如果有可能getWindowToken沒有查看?

+1

是這個錯別字 「getgetWindowToken()」? – MKJParekh

+1

[點擊EditText之外如何隱藏安卓軟鍵盤]可能的重複?(http://stackoverflow.com/questions/4165414/how-to-hide-soft-keyboard-on-android-after-clicking-outside -edittext) – Reno

+1

'InputMethodManager輸入管理=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);' 'inputManager.hideSoftInputFromWindow(findViewById(android.R.id.content).getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);' –

回答

15

當然,你可以使用:

getContentView().getWindowToken() 

,或者你可以參考SO Quest

+9

解決根據Hanry的建議:findViewById(android.R.id.content).getWindowToken() –

+1

它不起作用。 – technophyle

+6

沒有getContentView()方法。改爲使用findViewById(android.R.id.content).getWindowToken()。 – mhsmith

25

我面對完全一樣的問題,同時在活動中寫OnPageChangeListener。您可以使用這些解決方案之一。或者:

getWindow().getDecorView().getRootView().getWindowToken() 

或:

findViewById(android.R.id.content).getWind‌​owToken() 
0
public static final String M_TOKEN = "mToken"; 

@Nullable 
protected IBinder getToken(Activity activity) { 
    try { 
     Field mTokenField = Activity.class.getDeclaredField(M_TOKEN); 
     mTokenField.setAccessible(true); 
     IBinder mToken = (IBinder) mTokenField.get(activity); 
     return mToken; 
    } catch (NoSuchFieldException e) { 
     // handle 
    } catch (IllegalAccessException e) { 
     // handle 
    } 
    return null; 
} 
0

你可以試試這個在您的清單文件活動代碼隱藏鍵盤。

android:windowSoftInputMode="stateHidden" 
1

只需使用getWindow().getDecorView().getWindowToken()