2011-09-12 16 views

Respuesta

42

Se necesita un TextWatcher

lo ve aquí en acción:

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

Implementar un TextWatcher. Le da tres métodos, beforeTextChanged, onTextChanged y afterTextChanged. El último método no se debe invocar hasta que algo cambie de todos modos, por lo que es bueno usarlo.

8

Si cambia de opinión a escuchar las pulsaciones de teclado que puede utilizar 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; 
     } 
    }); 

Pero la respuesta de Jolie es lo que necesita.

1

En realidad, esto funcionó para mí

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) { 

    } 
}); 
Cuestiones relacionadas