2014-10-08 86 views

回答

2

您可以通過添加一個OnKeyListener您的EditText實現這一點,這裏是代碼片段:

myEditText.setOnKeyListener(new OnKeyListener() { 
      @Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
       // TODO Auto-generated method stub 
       if (keyCode == KeyEvent.KEYCODE_ENTER) { 
        // The "done" key is pressed, handle the event here. 
       } 
       return false; 
      } 

     }); 

您還可以通過一個OnEditorActionListener設置爲您的EditText實現這一目標。這裏是代碼:

myEditText.setImeOptions(EditorInfo.IME_ACTION_DONE); 
myEditText.setOnEditorActionListener(new OnEditorActionListener() { 

    @Override 
    public boolean onEditorAction(TextView arg0, int keycode, KeyEvent event) { 
       if (actionId == EditorInfo.IME_ACTION_DONE) { 
       // The "done" key is pressed, handle the event here. 
       } 
     return false; 
    } 

}); 
+0

我覺得這個答案沒有考慮到我的問題。這段代碼允許我處理按下Enter鍵完成的操作。我需要做的就是「欺騙」程序,認爲即使沒有按鍵,按鍵仍然被按下。然而,這個答案是有幫助的,因爲它給了我一個ID,我可以稍後再嘗試。 – Noob 2014-10-08 18:31:57

相關問題