2014-02-17 36 views
1

我正在編寫應用程序,在此應用程序中我編輯了文本。我想當用戶在編輯文本末尾寫入一些文本然後按下回車按鈕時,我想讓它調用一些命令。這是我所做的。這是ICS的工作,但是當我嘗試其他設備(Jelly Bean)時,它不起作用。在果凍豆上處理「Enter」鍵

inputViaTextChatbot.setOnEditorActionListener(new OnEditorActionListener() { 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) { 
       // hide the keyboard 
       InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 
       // process 
       getThis = inputViaTextChatbot.getText().toString(); 
       if (getThis!=null && getThis.length()>1) { 
        try { 
        Log.v("Got This: ", getThis); 
        } catch (IllegalStateException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 

        inputViaTextChatbot.setText(""); 
       } 
      }  
      return false; 
     } 
    }); 

任何人都可以幫助我做到這一點?

回答

2

這是一個已知的問題,使得Enter密鑰在多個設備上無法識別。一種解決方法,以避免它,並讓它工作將是以下幾點:

創建TextView.OnEditorActionListener這樣的:

TextView.OnEditorActionListener enterKey = new TextView.OnEditorActionListener() { 
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) { 
    if (actionId == EditorInfo.IME_ACTION_GO) { 
     // Do whatever you need 
    } 
    return true; 
    } 
}; 

假設你ViewEditText,例如,你需要這樣設置:

final EditText editor = (EditText) findViewById(R.id.Texto); 
editor.setOnEditorActionListener(enterKey); 

的最後一步走的是分配下列屬性的EditText

android:imeOptions="actionGo" 

這基本上改變了輸入鍵的默認行爲,將其設置爲actionGo IME選項。在您的處理程序中,只需將其分配給您創建的偵聽器,這樣您就可以獲得enter key的行爲。