我有5個addTextChangedListener用於5個編輯文本。我想在一個函數中自定義它們,這樣我就只需傳遞editttext的id,然後addTextChangedListener就可以應用了。我該怎麼辦這我沒有得到這個。請幫助我。在函數中設置addTextChangedListener Android
代碼
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系統定製它的功能,我們必須只是傳遞上並addTextChangedListener的ID將上
作爲應用建議
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
}
}
設置我沒有得到我怎麼會從我的更新現在我怎麼會從我的活動類調用這個和我的代碼活動 – Developer
調用此什麼是適配器 – Developer
@Gaurav,對不起,更新我的答案並刪除了適配器。那是爲了我自己的代碼。您將像這樣調用它,'mobileNumber2 =(EditText)findViewById(R.id.text)和mobileNumber2.addTextChangedListener(new addListenerOnTextChange(this,mobileNumber2);' – linus