Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android EditText - finished typing event

I want to catch an event when the user finishes editing EditText.

How can it be done?

like image 379
eddyuk Avatar asked Nov 09 '11 10:11

eddyuk


2 Answers

Better way, you can also use EditText onFocusChange listener to check whether user has done editing: (Need not rely on user pressing the Done or Enter button on Soft keyboard)

 ((EditText)findViewById(R.id.youredittext)).setOnFocusChangeListener(new OnFocusChangeListener() {      @Override     public void onFocusChange(View v, boolean hasFocus) {        // When focus is lost check that the text field has valid values.        if (!hasFocus) { {          // Validate youredittext       }     }  }); 

Note : For more than one EditText, you can also let your class implement View.OnFocusChangeListener then set the listeners to each of you EditText and validate them as below

((EditText)findViewById(R.id.edittext1)).setOnFocusChangeListener(this); ((EditText)findViewById(R.id.edittext2)).setOnFocusChangeListener(this);      @Override     public void onFocusChange(View v, boolean hasFocus) {        // When focus is lost check that the text field has valid values.        if (!hasFocus) {         switch (view.getId()) {            case R.id.edittext1:                  // Validate EditText1                  break;            case R.id.edittext2:                  // Validate EditText2                  break;         }       }     } 
like image 50
Vinayak Bevinakatti Avatar answered Sep 23 '22 12:09

Vinayak Bevinakatti


When the user has finished editing, s/he will press Done or Enter

((EditText)findViewById(R.id.youredittext)).setOnEditorActionListener(     new EditText.OnEditorActionListener() {         @Override         public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {             if (actionId == EditorInfo.IME_ACTION_SEARCH ||                     actionId == EditorInfo.IME_ACTION_DONE ||                     event != null &&                     event.getAction() == KeyEvent.ACTION_DOWN &&                     event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {                 if (event == null || !event.isShiftPressed()) {                    // the user is done typing.                      return true; // consume.                 }                             }             return false; // pass on to other listeners.          }     } ); 
like image 36
Reno Avatar answered Sep 22 '22 12:09

Reno