好久之前解决的一个问题, 点击返回键 或者 点击键盘的 done 当时会发生控制再也无法操作的问题
在EditText中,可以使用setImeOptions()方法来来开启软键盘的”Done”按钮。
示例代码如下:editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
按下”Done”按钮的默认行为是关闭软键盘,但是我们可以通过EditText的setOnEditorActionListener()方法
EditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
// EditorInfo.IME_ACTION_GO android:imeOptions="actionGo"
// EditorInfo.IME_ACTION_SEARCH android:imeOptions="actionSearch"
// EditorInfo.IME_ACTION_SEND android:imeOptions="actionSend"
// EditorInfo.IME_ACTION_NEXT android:imeOptions="actionNext"
// EditorInfo.IME_ACTION_UNSPECIFIED android:imeOptions="actionUnspecified"
if (actionId == EditorInfo.IME_ACTION_DONE) { android:imeOptions="actionDone"
return true;
}
return false;
}
EditorInfo.IME_ACTION_DONE可以和其他的标志一起组合使用来设置软键盘,比如:
editText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI|EditorInfo.IME_ACTION_DONE);
在输入法弹出的情况下 仍然接收处理 back
public class EditText1 extends EditText {
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
}
return super.dispatchKeyEventPreIme(event);
}
或者在 onKeyPreIme 方法
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK ){
this.clearFocus();
InputMethodManager inputMethodManager =(InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getWindowToken(), 0);
}
return super.onKeyPreIme(keyCode, event);
}