如何在Textwatcher上制作多个Editext?

时间:2016-10-25 05:36:24

标签: android

我有三个名为mobile,password,email的Edittext,我为它们实现了单独的Textwatcher,但是我想为多个Edittext制作单个Textwatcher。我怎么能这样做才能实现。

代码: -

private final TextWatcher m_EmailWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        checkForEmptyField();
    }


    @Override
    public void afterTextChanged(Editable s) {
    /*check whether SoftKeyPad open or hide*/
        m_MainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int heightDiff = m_MainLayout.getRootView().getHeight() - m_MainLayout.getHeight();
                //noinspection StatementWithEmptyBody
                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...

                } else {/*if hide then clear focus from Password Edit text*/
                    m_EmailEditText.clearFocus();
                    m_Email = m_EmailEditText.getText().toString().trim();
                    if (isValidEmail(m_Email) && m_Email.split("@")[0].length() <= 64 && (m_Email.split("@")[1].length() >= 2 && m_Email.split("@")[1].length() <= 255)) {

                    } else {
                        m_EmailEditText.setError("Please enter valid Email Id");
                    }

                }
            }
        });

    }
};
private final TextWatcher m_PassWordWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        checkForEmptyField();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};
private final TextWatcher m_MobileWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        checkForEmptyField();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
};

1 个答案:

答案 0 :(得分:1)

创建单独的Class扩展TextWatcher

public class CustomTextWatcher implements TextWatcher {

    private EditText editText;
    private Context context;
    private int value;

    public CustomTextWatcher(Context context, EditText et, int val){
        this.context = context;
        this.editText = et;
        this.value = val;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(s.toString().length() > 0){
            if(value == 0){
                //Email
            }
            else if(value == 1){
                //Mobile Number
            }
            else if(value == 2){
                //Password
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}

每当您想将TextWatcher应用于EditText时,请执行此操作。

CustomTextWatcher cew1 = new CustomTextWatcher(this, emailEditText, 0);
emailEditText.addTextChangedListener(cew1);