我在Android App开发方面有问题,我有一个TextWatcher
的类。但是TextWatcher无法使用getSelectionStart()
,getSelectionEnd()
,length()
,findViewById(mInputLayout)
,setText(s)
和setSelection(tempSelection)
的方法。主activity不能将值转移到customTextWatcher
中。你能帮我吗?
public class customTextWatcher implements TextWatcher {
CharSequence temp;
int editStart;
int editEnd;
String mEditText; //EditText
String mInputLayout; //Input Layout
int tempLength; // Set the EditText length to limit it
//private String activity; // Set the Activity of the class to use this class
String errorText; // When out of boundary with EditText , it will show the errorText
// Constructor for this Class,
// Get the params from main Class
public customTextWatcher(String mEditText, String mInputLayout, int tempLength, String errorText) {
this.mEditText = mEditText;
this.mInputLayout = mInputLayout;
this.tempLength = tempLength;
//this.activity = activity;
this.errorText = errorText;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
temp = s;
}
@Override
public void afterTextChanged(Editable s) {
// String selectedText = mEditText.getText().substring(editStart, editEnd);
editStart = mEditText.getSelectionStart();
editEnd = mEditText.getSelectionEnd();
if (tempLength.length() > 10) {
TextInputLayout til = (TextInputLayout)findViewById(mInputLayout);
til.setErrorEnabled(true);
til.setError(errorText);
s.delete(editStart - 1, editEnd);
int tempSelection = editStart;
mEditText.setText(s);
mEditText.setSelection(tempSelection);
}
}
}
mEditTextLastName = (EditText) findViewById(R.id.input_lastName);
mEditTextFirstName = (EditText) findViewById(R.id.input_firstName);
mEditTextHomeAddress = (EditText) findViewById(R.id.input_homeAddress);
mEditTextCountry = (EditText) findViewById(R.id.input_country);
mEditTextPhoneCode = (EditText) findViewById(R.id.input_PhoneCode);
mEditTextMobile = (EditText) findViewById(R.id.input_mobile);
mEditTextOtherPhone = (EditText) findViewById(R.id.input_otherPhone);
mEditTextEmail = (EditText) findViewById(R.id.input_email);
mEditTextPassword = (EditText) findViewById(R.id.input_password);
mEditTextReComfirmPassword = (EditText) findViewById(R.id.input_reConfirmPassword);
mEditTextWechat = (EditText) findViewById(R.id.input_wechat);
/**
* mTextInputLayout findById of layout TextInputLayout
*/
mTextInputLayout_LastName = (TextInputLayout) findViewById(R.id.lastNameLayout);
mTextInputLayout_FistName = (TextInputLayout) findViewById(R.id.firstNameLayout);
mTextInputLayout_HomeAddress = (TextInputLayout) findViewById(R.id.homeAddressLayout);
mTextInputLayout_Country = (TextInputLayout) findViewById(R.id.countryLayout);
mTextInputLayout_PhoneCode = (TextInputLayout) findViewById(R.id.phoneCodeLayout);
mTextInputLayout_Mobile = (TextInputLayout) findViewById(R.id.mobileLayout);
mTextInputLayout_OtherPhone = (TextInputLayout) findViewById(R.id.otherPhoneLayout);
mTextInputLayout_Email = (TextInputLayout) findViewById(R.id.emailAddressLayout);
mTextInputLayout_Password = (TextInputLayout) findViewById(R.id.passwordLayout);
mTextInputLayout_ReComfirmPassword = (TextInputLayout) findViewById(R.id.reConfirmPasswordLayout);
mTextInputLayout_Wechat = (TextInputLayout) findViewById(R.id.wechatLayout);
mEditTextLastName.addTextChangedListener(new customTextWatcher(mEditTextLastNam,mTextInputLayout_LastName,20,"error : Can't over 20's character!"));
这是因为您将文本观察者移至单独的类中。如果您的文本观看者是活动中的内部类,则可以访问该活动(上下文(。一种解决方案是定义文本观察者中的回调接口并在活动中实现。通过这样做,您将能够将活动设置为TextWatcher和Access Actives的方法的回调。