我有 5 个 addTextChangedListener 用于 5 个编辑文本。我想在一个函数中自定义它们,这样我就必须传递 editttext 的 id 并将 addTextChangedListener 应用于那个。我怎么能这样做我不明白这个。请帮助我对此。
代码
mobileNumber.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mobileNumber.setError(null);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
mobileNumber.setError(null);
}
});
mobileNumber2.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mobileNumber2.setError(null);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
mobileNumber2.setError(null);
}
});
mobileNumber1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mobileNumber1.setError(null);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
mobileNumber1.setError(null);
}
});
所有 addTextChangedListener 都在为不同的 id 做同样的事情。所以这在 android 中是否可以将它自定义为一个函数,我们只需传递 id 并且 addTextChangedListener 将应用于它
正如建议的那样
public class BookingClassAddListenerOnTextChange implements TextWatcher {
private Context mContext;
EditText mEdittextview;
public BookingClassAddListenerOnTextChange(Context context, EditText edittextview) {
super();
this.mContext = context;
this.mEdittextview= edittextview;
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
public void addTextChangedListener(
BookingClassAddListenerOnTextChange bookingClassAddListenerOnTextChange) {
// TODO Auto-generated method stub
}
}