Search code examples
javaandroidandroid-textwatcher

Android new class TextWatcher cannot tran value from mainActivity


I have the problem on android app development, I have a class to TextWatcher. but the textWatcher cannot use the method of getSelectionStart(), getSelectionEnd(), length(), findViewById(mInputLayout), setText(s) and setSelection(tempSelection). and the mainActivity cannot tran the value into customTextWatcher. Could you help me please.

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!"));

Solution

  • That is because you moved your TextWatcher into separate class. If your TextWatcher is inner class within your Activity, you can access that Activity (Context). One solution is to define callback interfaces in your TextWatcher and implement it in Activity. By doing so, you will be able to set your Activity as callback for TextWatcher and access Activity's methods.