版權聲明:本文為博主原創文章,未經博主允許不得轉載。
在我們開發EditText的時候,會經常和系統輸入框打交道,各種系統輸入框的顯示和隱藏的控制。常常也碰到各種顯示不了,隱藏不了的問題。最近碰到一個系統輸入框一直顯示不了的問題。
問題:自定義密碼輸入框顯示后,系統輸入框需要顯示,密碼輸入完畢后系統輸入框需要消失,這個時候,不管怎么樣都消失不了。
上網找了很多解決方案,對于我這個情況都沒有效果。稍微總結下:
1
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
2
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0); //強制隱藏鍵盤
3
將EditText的父級控設置成:
Android:focusable=”true”
android:focusableInTouchMode=”true”
4
在AndroidManifest.xml中的application中添加如下第一行代碼
android:windowSoftInputMode=”adjustPan|stateHidden”
5
if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
if (getCurrentFocus() != null)
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
.......等等諸如此類的方法對于一般的情況是有效果的。但是對于我的這個情況,不管是在onDestroy()方法里還是在輸入框需要消失的時候去調這些方法都是沒有用。后來我想到了會不會是焦點的問題,輸入框不消失肯定是有某個控件或者view獲得了焦點,于是,想到了如下的解決方案。
v.requestFocus();
if (v != null) {
IBinder token = v.getWindowToken();
if (token != null) {
InputMethodManager im = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(token, 0);
}
}
其中的v可以是任意需要獲取焦點的view。經測試,能解決很多系統輸入框不能消失的問題。