我引用了这个link来监听android EditText完成输入事件。但是从这个参考我只得到 Enter 键按下事件。当用户在完成打字后关闭键盘时以及用户按下一步时,我需要听一个事件。有没有办法在android中听这些事件。
答案 0 :(得分:3)
最后我得到了下一个/完成/搜索的答案并按此键输入按键
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(event !=null){
if(actionId == EditorInfo.IME_ACTION_SEARCH ||
actionId == EditorInfo.IME_ACTION_NEXT ||
actionId == EditorInfo.IME_ACTION_DONE ||
event.getAction() == KeyEvent.ACTION_DOWN &&
event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!event.isShiftPressed()) {
Log.e("", "FINISHED Typing : "+v.getText().toString());
return true; // consume.
}
}
}
else{
if(actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE){
Log.e("", "Next/Done/Search Pressed");
return true;
}
}
return false;
}
对于键盘解雇我也自定义了editText并将onKeyPreIme方法覆盖到其中。每当我需要edittext时,我都在使用这个edittext。这解决了我的问题。
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (event.getAction()!=KeyEvent.ACTION_DOWN){
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
dispatchKeyEvent(event);
//do whatever you want to do here, when keypad dismiss on EditText
return false;
}
}
return super.onKeyPreIme(keyCode, event);
}