在項目開發過程中,界面包含EditText輸入框時,輸入內容時軟鍵盤彈起。但是此時我們調用界面中自定義的返回按鈕執行.finish()方法銷毀Activity后發現軟鍵盤并沒有跟隨關閉收回,參考了網上各種方法后,試驗總結出自己項目中可用的方案。
注意:此篇記錄的是調用.finish()返回時軟鍵盤未回收問題,而非手機本身的back鍵(手機本身的back鍵點擊是會先收回軟鍵盤,在銷毀Activity)。
1、在AndroidManifest.java清單文件的標簽下加入stateUnspecified。
使用環境是在ActivityB開啟ActivityC,ActivityC中包含EditText,點擊輸入框獲取焦點使軟鍵盤彈出,然后調用自定義返回按鈕執行.finish()方法,當ActivityC銷毀后返回到ActivityB界面時,軟鍵盤未回收問題,此時可以在AndroidManifest.java的ActivityB的標簽下添加android:windowSoftInputMode="stateUnspecified"。
注:此方案在我的項目中使用無效,在此記錄。
B界面設置android:windowSoftInputMode
鍵盤是否自動回收
stateUnspecified
?
stateUnchanged
?
stateHidden
?
stateAlwaysHidden
?
stateVisible
?
stateAlwaysVisible
?
stateHidden
?
不指定
?
2、在Activity的onDestory方法中監測軟鍵盤是否存在,存在則回收。
備注:此方法不穩定,在Activity中不包含EditText時會莫名其妙的彈起軟鍵盤。
@Override
protected void onDestroy() {
super.onDestroy();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
}
}
3、在.finish()前先收回軟鍵盤
在每個Activity中調用.finish()前手動調用收回軟鍵盤方法,此方案測試過幾個頁面,可以使用。在使用時最好判斷軟鍵盤是否有顯示,若顯示則回收,否則忽略。
@OnClick({R.id.iv_back)
void iv_depart_back(View view){
hideInput()
finish();
}
protected void hideInput() {
super.onDestroy();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
}
}
4、自定義View,覆寫dispatchKeyEventPreIme(KeyEvent event)方法
此方案未測試,針對現有的項目不好再去改動之前已經定義好的View內容,所以是新開啟項目的可以嘗試使用。
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.RelativeLayout;
/**
* Finishes the containing activity on BACK, even if input method is showing.
*/
public class SearchActivityView extends RelativeLayout {
public SearchActivityView(Context context) {
super(context);
}
public SearchActivityView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SearchActivityView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private Activity getActivity() {
Context context = getContext();
if (context instanceof Activity) {
return (Activity) context;
} else {
return null;
}
}
/**
* Hides the input method.
*/
protected void hideInputMethod() {
InputMethodManager imm = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(getWindowToken(), 0);
}
}
/**
* Overrides the handling of the back key to dismiss the activity.
*/
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
Activity activity = getActivity();
if (activity != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
state.startTracking(event, this);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP && !event.isCanceled() && state.isTracking(event)) {
hideInputMethod();
activity.onBackPressed();
return true;
}
}
}
return super.dispatchKeyEventPreIme(event);
}
}
5、設置自定義的返回按鈕響應系統的back鍵,在點擊事件中執行
此方式需要在線程中執行,未進行測試。
new Thread () {
public void run () {
try {
Instrumentation inst= new Instrumentation();
inst.sendKeyDownUpSync(KeyEvent. KEYCODE_BACK);
} catch(Exception e) {
e.printStackTrace();
}
}
}.start();
6、最終解決方案(方案3改良版本)
方案3中的方式可以用,但是有弊端,需要每個Activity中單獨添加,如果之前在Base基類中定義過統一的關閉方式修改還是比較方便的。但是如果沒有,也沒關系。我們可以在Base基類中覆寫finisi()方法,來關閉軟鍵盤。
@Override
public void finish() {
super.finish();
hideKeyBoard();
}
/**
* 關閉軟鍵盤
*/
public void hideKeyBoard() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive()&&getCurrentFocus()!=null){
if (getCurrentFocus().getWindowToken()!=null) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}
總結:上述方案1、2、3都測試過,在不同機型上使用效果都有差異,最后的方案6在現有的測試機上測試都可以正常顯示和關閉,在此進行記錄。