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) {}
});
}
}
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();
}
}
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());
}
});
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);
}
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);
}
});
public static boolean comS(String s1,String s2){
if (s1.length()==s2.length()){
int l=s1.length();
for (int i=0;i<l;i++){
if (s1.charAt(i)!=s2.charAt(i))return false;
}
return true;
}
return false;
}
public void onChange(final EditText EdTe, final Runnable FRun){
class finalS{String s="";}
final finalS dat=new finalS();
EdTe.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {dat.s=""+EdTe.getText();}
else if (!comS(dat.s,""+EdTe.getText())){(new Handler()).post(FRun);}
}
});
}
要使用它,只需这样调用:
onChange(YourEditText, new Runnable(){public void run(){
// V V YOUR WORK HERE
}}
);
val searchTo : EditText = findViewById(R.id.searchTo)
searchTo.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
// you can call or do what you want with your EditText here
// yourEditText...
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
})
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public abstract class NumberChangedListener implements TextWatcher {
private final EditText target;
private final String defaultValue;
public NumberChangedListener(EditText target, int defaultValue) {
this.target = target;
this.defaultValue = defaultValue + "";
}
@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);
}
private void onTextChanged(EditText target, Editable s) {
String input = s.toString();
String number = input.replaceAll("[^\\d]", "");
if (!number.equals(input)) {
target.setText(number);
return;
}
Integer integer;
try {
integer = Integer.valueOf(number);
} catch (NumberFormatException ignored) {
target.setText(defaultValue);
return;
}
if (!integer.toString().equals(number)) {
target.setText(integer.toString());
return;
}
onNumberChanged(integer);
}
public abstract void onNumberChanged(int value);
}
用作
int defaultVal = 10;
mTextView.addTextChangedListener(new NumberChangedListener(mTextView, defaultVal) {
@Override
public void onNumberChanged(int value) {
// use the parsed int
}
});