10

我有 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

    }

}
4

2 回答 2

16

您应该像这样创建一个侦听器类,只需修改构造函数中的参数以接受您要添加侦听器的 EditText ID。

public class addListenerOnTextChange implements TextWatcher {
    private Context mContext;
    EditTextView mEdittextview;

    public addListenerOnTextChange(Context context, EditTextView edittextview) {
        super();
        this.mContext = context;
        this.mEdittextview= edittextview;
    }

    @Override
    public void afterTextChanged(Editable s) {  
    }

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

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //What you want to do
        }
    }
}

您在活动中这样称呼他们

mobileNumber2.addTextChangedListener(new addListenerOnTextChange(this, mobileNumber2));

根据需要再次修改参数。

于 2013-08-29T06:52:01.817 回答
3

如果所有五个的行为方式都相同,那么您可以创建一个 TextWatcher 对象并将其传递给所有对象。

TextWatcher tw = new TextWatcher() {
    /* code goes here */
    /* viewOnFocus can be used here */
}

mobilenum1.addTextChangedListener(tw);
mobilenum2.addTextChangedListener(tw);
mobilenum3.addTextChangedListener(tw);

viewOnFocus变量是通过onFocusChange侦听器设置的

OnFocusChangeListener of = new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            viewInFocus = view;
        }
    }
};
mobile1.setOnFocusChangeListener(of);
mobile2.setOnFocusChangeListener(of);
mobile3.setOnFocusChangeListener(of);
于 2013-08-29T06:53:47.800 回答