Android edittext onchange listener

I know a little about TextWatcher , but it works on every character you enter. I want the listener to start whenever the user finishes editing. Is it possible? Also in TextWatcher I get an instance of Editable , but I need an instance of EditText . How to get it?

EDIT : The second question is more important. Answer that.

+76
android android-edittext textwatcher
Jun 21 '12 at 8:17
source share
7 answers

firstly, you can see if the user has finished editing the text if EditText loses focus or if the user clicks the completion button (it depends on your implementation and what suits you best). Secondly, you cannot get an EditText instance inside a textwatcher only if you declared EditText as an instance object. Even if you do not have to edit EditText in textwatcher because it is not safe.

EDIT:

To get an instance of EditText in your TextWatcher implementation, you should try something like this:

 public class YourClass extends Activity { private EditText yourEditText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); yourEditText = (EditText) findViewById(R.id.yourEditTextId); yourEditText.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { // you can call or do what you want with your EditText here yourEditText. ... } public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence s, int start, int before, int count) {} }); } } 

Please note that the above example may have some errors, but I just wanted to show you an example.

+154
Jun 21 '12 at 8:23
source share

Anyone who uses ButterKnife . You can use as:

 @OnTextChanged(R.id.zip_code) void onZipCodeTextChanged(CharSequence zipCode, int start, int count, int after) { } 
+8
Jun 17 '16 at 17:05
source share

I was worried that implementing a listener for all EditText fields required me to have ugly, detailed code, so I wrote a class below. It may be useful to anyone who stumbles upon this.

 public abstract class TextChangedListener<T> implements TextWatcher { private T target; public TextChangedListener(T target) { this.target = target; } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) {} @Override public void afterTextChanged(Editable s) { this.onTextChanged(target, s); } public abstract void onTextChanged(T target, Editable s); } 

The listener implementation is now a little cleaner.

 editText.addTextChangedListener(new TextChangedListener<EditText>(editText) { @Override public void onTextChanged(EditText target, Editable s) { //Do stuff } }); 

As for how often it works, one could implement a check to run your desired code in //Do stuff after a given

+7
Sep 21 '16 at 19:05
source share

I did this using AutotextView :

 AutotextView textView = (AutotextView) findViewById(R.id.autotextview); textView.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { seq = cs; } @Override public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3) { } @Override public void afterTextChanged(Editable arg0) { new SearchTask().execute(seq.toString().trim()); } }); 
+4
May 12 '14 at 7:26
source share

TextWatcher did not work for me, as it continued to shoot for each EditText and ruin each other's values.

Here is my solution:

 public class ConsultantTSView extends Activity { ..... //Submit is called when I push submit button. //I wanted to retrieve all EditText(tsHours) values in my HoursList public void submit(View view){ ListView TSDateListView = (ListView) findViewById(R.id.hoursList); String value = ((EditText) TSDateListView.getChildAt(0).findViewById(R.id.tsHours)).getText().toString(); } } 

Therefore, using the getChildAt(xx) method, you can get any item in a ListView and get a single item using findViewById . And then he will give the latest meaning.

+2
Jul 11 '13 at 0:45
source share
  myTextBox.addTextChangedListener(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) { TextView myOutputBox = (TextView) findViewById(R.id.myOutputBox); myOutputBox.setText(s); } }); 
+2
Nov 04 '16 at 10:05
source share

As far as I can judge it, there are only two ways to do this. How do you know that the user has finished writing the word? Either we lost focus or by clicking the "OK" button. In my opinion, you may not know that the user clicked the last character ...

So, call onFocusChange(View v, boolean hasFocus) or add a button and a click listener to it.

+1
Jun 21 '12 at 8:21
source share



All Articles