2009-11-04 106 views
11

與物理鍵盤,您可以趕上一個KeyListener按鍵,類似:在Android中使用虛擬鍵盤按下捕捉鍵?

myEditText.setOnKeyListener(new OnKeyListener() { 
    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_ENTER) { 
      /* do something */ 
     } 
    } 
}); 

有誰知道如何做到這一點(或類似)與虛擬鍵盤?

+3

你不覺得這會是一個更好的主意,分享你找到了更好的辦法,我的意思是我來這裏尋求幫助,制定出回答你的自我,然後不要放棄答案,更給少拿我想;) – Spider 2013-01-23 23:53:34

回答

24

到目前爲止,我還沒有在android中找到任何虛擬鍵盤的監聽器。我找到了一個替代解決方案,即我使用TextChanged事件來檢索編輯文本中輸入的鍵的值。

import android.app.Activity; 
    import android.os.Bundle; 
    import android.text.Editable; 
    import android.text.TextWatcher; 
    import android.util.Log; 
    import android.view.KeyEvent; 
    import android.view.View; 
    import android.view.View.OnKeyListener; 
    import android.widget.EditText; 
    import android.widget.TextView; 
    import android.widget.Toast; 

    public class ShowKeypad extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      EditText emailTxt = (EditText) findViewById(R.id.editText); 

      emailTxt.addTextChangedListener(new TextWatcher() 
      { 
        public void afterTextChanged (Editable s){ 
          Log.d("seachScreen", "afterTextChanged"); 
        } 
        public void beforeTextChanged (CharSequence s, int start, int 
          count, int after) 
        { 
          Log.d("seachScreen", "beforeTextChanged"); 
        } 
        public void onTextChanged (CharSequence s, int start, int before, 
          int count) 
        { 
          Log.d("seachScreen", s.toString()); 
        } 

      final TextView tv = (TextView)findViewById(R.id.tv); 
    }); 
    } 

} 
+2

如果在editText中沒有輸入的文本?你如何看待處理刪除密鑰? – atasoyh 2014-06-17 07:11:13