Skip to content Skip to sidebar Skip to footer

How Do Listen Edittext?

I have a EditText. I wamt tp do something, when the user presses the Enter key while changing EditText. How can I do that? The most simplest method: final EditText edittext = (Edi

Solution 1:

sample code for text watcher

your_edittext.addTextChangedListener(newInputValidator());

    privateclassInputValidatorimplementsTextWatcher {

        publicvoidafterTextChanged(Editable s) {

        }    
        publicvoidbeforeTextChanged(CharSequence s, int start, int count,
                int after) {                

        }    
        publicvoidonTextChanged(CharSequence s, int start, int before,
                int count) {

        }    
    }    
}

Solution 2:

First, create an OnEditorActionListener (as a private instance variable, for example):

private TextView.OnEditorActionListenermEnterListener=newTextView.OnEditorActionListener() {
        @OverridepublicbooleanonEditorAction(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 */
            }
        returntrue;
    });

Then, set the listener (i.e. in your onCreate method):

EditTextmEditText= (EditText) findViewById(...);
mEditText.setOnEditorActionListener(mEnterListener);

Solution 3:

Another alternative is:

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

       }
      });

Solution 4:

You need to use TextWatcher for this purpose. Here is an example on how to work with TextWatcher and Android textwatcher API.

Solution 5:

your_edittext.setOnClickListener(newView.OnClickListener() {
    @OverridepublicvoidonClick(View v) {
        //do something
    }

});

Post a Comment for "How Do Listen Edittext?"