2011-09-12 30 views

Trả lời

42

Bạn cần một TextWatcher

Xem nó ở đây trong hành động:

EditText text = (EditText) findViewById(R.id.YOUR_ID); 
text.addTextChangedListener(textWatcher); 


private TextWatcher textWatcher = new 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

Thực hiện một TextWatcher. Nó cung cấp cho bạn ba phương thức, beforeTextChanged, onTextChangedafterTextChanged. Phương pháp cuối cùng không nên được gọi cho đến khi một cái gì đó thay đổi anyway, vì vậy đó là một điều tốt để sử dụng cho nó.

8

Nếu bạn thay đổi suy nghĩ của bạn để lắng nghe các tổ hợp phím, bạn có thể sử dụng OnKeyListener

EditText et = (EditText) findViewById(R.id.search_box); 

    et.setOnKeyListener(new View.OnKeyListener() { 

     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      //key listening stuff 
      return false; 
     } 
    }); 

Nhưng câu trả lời Johe là những gì bạn cần.

1

Điều này thực sự hiệu quả đối với tôi

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

text.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

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

     if(your_string.equals(String.valueOf(s))) { 
      //do something 
     }else{ 
      //do something 
     } 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 

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