前言,对网上的解决方案的补充
参考:https://blog.csdn.net/qq_33362988/article/details/76269448
业务中我需要大量的使用editText,有近100个要处理,固然每个控件都可以用这种方法处理,这样会形成大量的臃肿代码,也太不软件工程了。下面是我对该方案进行提取形成了一个方法,看起来更加简洁
工具类
public class AdpUtil {public static void removeTextWacth(List<EditText> editTextList) {int size = editTextList.size();for (int i = 0; i < size; i++) {if (editTextList.get(i).getTag() instanceof TextWatcher) {editTextList.get(i).removeTextChangedListener((TextWatcher) editTextList.get(i).getTag());}}}public static void setEditTextWatcher(EditText editText, final IBeanSetData iBeanSetData) {//处理editText的textWatcher的问题TextWatcher mWacher = new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {iBeanSetData.setData(s.toString());}};editText.addTextChangedListener(mWacher);editText.setTag(mWacher);}//主要是这里,3个步骤都进行了处理,editText只需要使用一遍即可public static void setEditTextWatcher(EditText editText, final IBeanSetData iBeanSetData,final IBeanGetData iBeanGetData) {//1.处理editText的textWatcher的问题//2.editText设值//3.返回editTextListeditText.removeTextChangedListener((TextWatcher) editText.getTag());editText.setText(iBeanGetData.getData());TextWatcher mWacher = new TextWatcher() {@Overridepublic void beforeTextChanged(CharSequence s, int start, int count, int after) {}@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void afterTextChanged(Editable s) {iBeanSetData.setData(s.toString());}};editText.addTextChangedListener(mWacher);editText.setTag(mWacher);}public interface IBeanSetData<T> {void setData(T s);}public interface IBeanGetData {String getData();}}
如何使用时
//这里使用了jdk1.8引入的lambda表达式,进一步缩减了代码量AdpUtil.setEditTextWatcher(holder.evaluatePaperPart, s -> bean.setPaperPart(s.toString()),() -> evaPaperBean.getPaperPart());
本文链接:https://my.lmcjl.com/post/1341.html
展开阅读全文
4 评论