2013-06-30 55 views
3

我正在開發一個消息應用程序,其中我有一個EditText供用戶鍵入他的消息。我用setImeOptions()方法在鍵盤上提供了一個'發送'按鈕。但是,只要用戶點擊「發送」按鈕,EditText就會失去焦點。 (我對'焦點'這個詞有懷疑,但我的意思是鍵盤消失..)Android:EditText在'發送'後失去焦點

我覺得有點不方便,因爲用戶必須在每次發送後再次單擊EditText以獲取鍵盤。我在代碼試圖editText1.requestFocus()如下:

editText1.setOnKeyListener(new OnKeyListener() { 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
       // If the event is a key-down event on the "send" button 
       if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { 
        // Perform action on key press 
        adapter.add(new OneComment(false, editText1.getText().toString())); 
        editText1.setText(""); 
        editText1.requestFocus(); 
        return true; 
       } 
       return false; 
      } 
     }); 

但是,這並不工作...請提出一個workaround..thanks :)

回答

2

你可以嘗試這樣做

editText1.setOnEditorActionListener(new OnEditorActionListener() {   
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     if(actionId == EditorInfo.IME_ACTION_SEND){ 
      editText1.setText(""); 
      editText1.requestFocus(); 
      InputMethodManager imm = (InputMethodManager)getSystemService(Service.INPUT_METHOD_SERVICE); 
      imm.showSoftInput(editText1, 0); 
     } 
     return true; 
    } 
}); 
+0

鍵盤仍然消失:( – tigerden

+0

嘗試更新的代碼:s – stinepike

+0

非常感謝您的快速幫助:)但是Eclipse顯示錯誤getSystemService方法...即使導入Service和InputMethodManager..any想法? – tigerden