2013-01-12 44 views
3

我想後的功能的EditText和回車鍵

  1. 只需輸入 - 回報的EditText,並提交文字
  2. Shift + Enter - 在新的EditText線

這是代碼,但它不工作。輸入和Shift + Enter之間無差異(不換行):

 
EditText text=(EditText)findViewById(R.id.text); 

text.setOnEditorActionListener(new OnEditorActionListener() { 

     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 

      if ((actionId == EditorInfo.IME_ACTION_DONE) || 
       ( (event.isShiftPressed()==false) && 
       (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && 
       (event.getAction() == KeyEvent.ACTION_DOWN))){ 

       Editable buff=(Editable)v.getText(); 
       writeText(buff.toString()); 

       context.finish(); // texten sparad här o activity avslutas 

       return true; 
      } 
      return false; 
      } 
    }); 

在layout.xml:

android:inputType="text|textMultiLine" 
android:imeOptions="actionDone" 

回答

3

我認爲你需要使用shift鍵收聽,並保持一個布爾值,當檢測到您按下Shift鍵,像這樣:

Android shift key listener

@Override 
public boolean onKey(View v, int keyCode, KeyEvent event) 
{ 
    switch (v.getId()) 
    { 
     case R.id.myEditTextId: 
     if(keyCode==59)//59 is shift's keycode 
     //do your stuff here against pressing shift key 
     break; 
    } 
} 
+0

啊哈..謝謝。我會試試這個.. – user1114026