android:Softkeyboard按下完成键时执行操作

我有一个EditText。 我想要input一些文本后,当用户按下softkeybard的完成键,它应该执行一些search操作,我也实现了一个button点击事件。 怎么做…???

尝试这个

editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if(actionId==EditorInfo.IME_ACTION_DONE){ //do something } return false; } }); 

尝试这个

它适用于DONERETURN

 EditText editText= (EditText) findViewById(R.id.editText); editText.setOnEditorActionListener(new EditText.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER || actionId == EditorInfo.IME_ACTION_DONE) { // Do your action return true; } return false; } }); 

你赶上KeyEvent ,然后检查它的键码。 FLAG_EDITOR_ACTION用于标识来自IME的input键,其input键已经被自动标记为“下一个”或“完成”

 if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION) //your code here 

这里find文档。

第二种方法

 myEditText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { int result = actionId & EditorInfo.IME_MASK_ACTION; switch(result) { case EditorInfo.IME_ACTION_DONE: // done stuff break; case EditorInfo.IME_ACTION_NEXT: // next stuff break; } } }); 

尝试这个

无论您的键盘是显示input符号还是下一个箭头符号,这都可以工作

 YourEdittextName.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if(actionId== EditorInfo.IME_ACTION_DONE||actionId==EditorInfo.IME_ACTION_NEXT) { //Perform Action here } return false; } }); 

如果你面对红线,然后做这个…导入Keyevent和EditorInfo按alt + enter然后所有的错误,将其删除将正确…….