问题
I know a little bit about TextWatcher
but that fires on every character you enter. I want a listener that fires 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 do I get that?
EDIT: the second question is more important. Please answer that.
回答1:
First, you can see if the user finished editing the text if the EditText
loses focus or if the user presses the done button (this depends on your implementation and on what fits the best for you).
Second, you can't get an EditText
instance within the TextWatcher
only if you have declared the EditText
as an instance object. Even though you shouldn't edit the EditText
within the TextWatcher
because it is not safe.
EDIT:
To be able to get the EditText
instance into 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) {}
});
}
}
Note that the above sample might have some errors but I just wanted to show you an example.
回答2:
It was bothering me that implementing a listener for all of my EditText fields required me to have ugly, verbose code so I wrote the below class. May be useful to anyone stumbling 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);
}
Now implementing a listener is a little bit cleaner.
editText.addTextChangedListener(new TextChangedListener<EditText>(editText) {
@Override
public void onTextChanged(EditText target, Editable s) {
//Do stuff
}
});
As for how often it fires, one could maybe implement a check to run their desired code in //Do stuff
after a given a
回答3:
Anyone using ButterKnife. You can use like:
@OnTextChanged(R.id.zip_code)
void onZipCodeTextChanged(CharSequence zipCode, int start, int count, int after) {
}
回答4:
I have done it 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());
}
});
回答5:
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);
}
});
回答6:
TextWatcher
didn't work for me as it kept firing for every EditText
and messing up each others 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();
}
}
Hence by using the getChildAt(xx)
method you can retrieve any item in the ListView
and get the individual item using findViewById
. And it will then give the most recent value.
回答7:
As far as I can think bout it, there's only two ways you can do it. How can you know the user has finished writing a word? Either on focus lost, or clicking on an "ok" button. There's no way on my mind you can know the user pressed the last character...
So call onFocusChange(View v, boolean hasFocus)
or add a button and a click listener to it.
来源:https://stackoverflow.com/questions/11134144/android-edittext-onchange-listener