2011-09-28 25 views
51

我有一個EditText。我想在鍵入一些文本後,當用戶按下softkeybard的完成鍵時,它應該執行一些搜索操作,這也是我在一個按鈕單擊事件中實現的。怎麼做...???android:softkeyboard按Done鍵時執行動作

+0

更好的方式與Kotlin在此評論:https://stackoverflow.com/a/48810268/1912924 –

回答

139

試試這個

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

謝謝,它的工作... –

15

試試這個

這既適用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; 
       } 
      }); 
0

你抓住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; 
    } 
} 
}); 
0

試試這個

這將在兩個條件下工作,你的鍵盤是否顯示如果烏拉圭回合面臨的紅線進入標誌或下一個箭頭標誌

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 然後所有的錯誤刪除它將正確.......