2012-02-15 34 views
5

Tôi có một số EditText. Tôi không làm điều gì đó, khi người dùng nhấn phím Nhập trong khi thay đổi EditText. Làm thế nào tôi có thể làm điều đó?Làm thế nào để nghe EditText?

Phương pháp đơn giản nhất:

final EditText edittext = (EditText) findViewById(R.id.edittext); 
edittext.setOnKeyListener(new OnKeyListener() { 
    public boolean onKey(View v, int keyCode, KeyEvent event) { 
     // If the event is a key-down event on the "enter" button 
     if ((event.getAction() == KeyEvent.ACTION_DOWN) && 
      (keyCode == KeyEvent.KEYCODE_ENTER)) { 
      // Perform action on key press 
      Toast.makeText(HelloFormStuff.this, edittext.getText(), Toast.LENGTH_SHORT).show(); 
      return true; 
     } 
     return false; 
    } 
}); 
+0

ok, và làm cách nào tôi có thể so sánh CharSequence và Khóa nhập? – ruslanys

Trả lời

5

mẫu mã cho người theo dõi văn bản

your_edittext.addTextChangedListener(new InputValidator()); 

    private class InputValidator implements TextWatcher { 

     public void afterTextChanged(Editable s) { 

     }  
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) {     

     }  
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 

     }  
    }  
} 
-2
your_edittext.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     //do something 
    } 

}); 
+0

Không, nó không hoạt động. Tôi cần phải nghe phím ENTER trên bàn phím trong khi người dùng gõ một số văn bản trong EditText. Tôi không quan tâm khi nào người dùng sẽ chạm vào màn hình. – ruslanys

0

Bạn cần phải sử dụng TextWatcher cho mục đích này. Dưới đây là ví dụ về cách làm việc với TextWatcher và API Android textwatcher.

+0

Oh yeah! Cảm ơn nhiều. Đó là một quyết định tốt đẹp. – ruslanys

4

Đầu tiên, tạo một OnEditorActionListener (như là một biến tin dụ, ví dụ):

private TextView.OnEditorActionListener mEnterListener = 
    new TextView.OnEditorActionListener() { 
     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_DOWN) { 
       /* If the action is a key-up event on the return key, do something */ 
      } 
     return true; 
    }); 

Sau đó, thiết lập người nghe (tức là trong phương pháp onCreate của bạn):

EditText mEditText = (EditText) findViewById(...); 
mEditText.setOnEditorActionListener(mEnterListener); 
+0

Có một ví dụ về cách thực hiện điều này trong mã mẫu 'BluetoothChat': http://developer.android.com/resources/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChat.html –

+0

Phương pháp đó khó hơn trước, dù sao cũng cảm ơn bạn. – ruslanys

+0

Tôi xin lỗi phương pháp của bạn tốt hơn. Cảm ơn) – ruslanys

1

Một lựa chọn khác là:

your_edittext.addTextChangedListener(new TextWatcher() { 

     @Override 
     public void afterTextChanged(Editable s) {} 

     @Override  
     public void beforeTextChanged(CharSequence s, int start, 
     int count, int after) { 
     } 

     @Override  
     public void onTextChanged(CharSequence s, int start, 
     int before, int count) { 

     } 
     }); 
Các vấn đề liên quan