您需要創建自定義EditText以捕獲keyBoardDismissEvent。
你可以嘗試這樣的事情:
public class CustomEditText extends EditText {
private OnKeyboardDismissListener listener;
public void setOnKeyBoardDismissListener(OnKeyboardDismissListener listener) {
this.listener = listener;
}
public CustomEditText(Context context) {
super(context);
}
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && listener != null)
listener.onKeyBoardDismiss();
return super.onKeyPreIme(keyCode, event);
}
static interface OnKeyboardDismissListener {
void onKeyBoardDismiss();
}
}
在佈局
<YourPackageName.CustomEditText android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
內活動:
CustomEditText editText = (CustomEditText) findViewById(R.id.editText);
editText.setOnKeyBoardDismissListener(new OnKeyboardDismissListener() {
@Override
public void onKeyBoardDismiss() {
MainActivity.this.finish();
}
});
我會感到高興,如果你讀了我的問題完全。當鍵盤出現時,onBackPressed()不會被觸發。 – Kanth
我的不好,當我看到你的問題時,並沒有清楚地顯示出來...... –