android:softkeyboard按Done键时执行动作

问题描述:

我有一个EditText。我想在键入一些文本后,当用户按下softkeybard的完成键时,它应该执行一些搜索操作,这也是我在一个按钮单击事件中实现的。怎么做...???android:softkeyboard按Done键时执行动作

+0

更好的方式与Kotlin在此评论:https://*.com/a/48810268/1912924 –

试试这个

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

谢谢,它的工作... –

试试这个

这既适用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其回车键已经被自动标记“下一步”或“完成”未来的钥匙

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

中找到文档here

第二种方法

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; 
    } 
} 
}); 

试试这个

这将在两个条件下工作,你的键盘是否显示如果乌拉圭回合面临的红线进入标志或下一个箭头标志

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; 
     } 
    }); 

然后执行此操作... 通过按alt +键入输入Keyevent和EditorInfo 然后所有的错误删除它将正确.......