总结TextWatcher两种使用场景
来源:互联网 发布:什么是淘宝客鹊桥 编辑:程序博客网 时间:2024/06/05 16:39
1. 观察多个输入框(必填)是否输入内容,控制按钮是否可用。
public class ButtonWatcher { private final Button btn; //控制按钮 private final EditText[] views; //输入框数组 public ButtonWatcher(Button btn, EditText... editTexts) { this.btn = btn; this.views = editTexts; this.btn.setEnabled(false); //默认不可用 for (EditText v : editTexts) { v.addTextChangedListener(textWatcher); } } TextWatcher textWatcher = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { boolean flag = false; for (EditText v : views) { if (TextUtils.isEmpty(v.getText().toString())) { flag = true; break; } } btn.setEnabled(!flag); } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void afterTextChanged(Editable arg0) { } };}
xml布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/save" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector_btn_enabled" android:text="保存" /></LinearLayout>按钮背景
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 不可用背景 --> <item android:drawable="@color/btn_enabled_color" android:state_enabled="false" /> <!-- 默认背景 --> <item android:drawable="@color/btn_color" /></selector>代码调用
EditText nameEditText = (EditText) findViewById(R.id.name); EditText passwordEditText = (EditText) findViewById(R.id.password); Button saveButton = (Button) findViewById(save); saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { L.d("数据已全部填写,按钮可用, 执行保存"); } }); new ButtonWatcher(saveButton, nameEditText, passwordEditText);
2. 观察多个输入框内容是否改变。
public class ChangeWatcher { private boolean change; //内容是否改变 private final EditText[] views; //输入框数组 private List<String> originalTexts; //输入框原始数据 public ChangeWatcher(EditText... editTexts) { this(true, editTexts); } public ChangeWatcher(boolean enalbe, EditText... editTexts) { this.views = editTexts; this.originalTexts = new ArrayList<>(); for (EditText v : editTexts) { v.setEnabled(enalbe); v.addTextChangedListener(textWatcher); originalTexts.add(v.getText().toString()); } } TextWatcher textWatcher = new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { for (int i = 0; i < views.length; i++) { if (!originalTexts.get(i).equals(views[i].getText().toString())) { change = true; break; } else { change = false; } } } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void afterTextChanged(Editable arg0) { } }; public boolean isChange() { return change; } public void setEditViewEnabled(boolean enable) { for (EditText v : views) { v.setEnabled(enable); } }代码调用
Button saveButton = (Button) findViewById(save); EditText nameEditText = (EditText) findViewById(R.id.name); EditText passwordEditText = (EditText) findViewById(R.id.password); nameEditText.setText("名字"); passwordEditText.setText("12345678"); final ChangeWatcher changeWatcher = new ChangeWatcher(nameEditText, passwordEditText); saveButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (changeWatcher.isChange()){ L.d("数据改变,执行更新保存"); } } });
阅读全文
0 0
- 总结TextWatcher两种使用场景
- FutureTask的用法及两种常用的使用场景
- FutureTask的用法及两种常用的使用场景
- 在前端使用JSON两种常用场景的分析
- git 使用场景总结
- Git使用场景总结
- Retrofit使用场景总结
- ZooKeeper使用场景总结
- KVC使用场景总结
- 使用TextWatcher监听EditText
- TextWatcher的使用
- TextWatcher的使用
- Android TextWatcher使用详解
- TextWatcher
- TextWatcher
- TextWatcher()
- TextWatcher
- TextWatcher
- 开启memcached日志
- 华容道
- 05.22 LNMP组合架构
- Windows与Linux文件共享
- 纪中5号~16号集训小结
- 总结TextWatcher两种使用场景
- Java 8新增的Lambda表达式
- SQL SERVER創建流水號的存儲過程
- 奥威Power-BI数据可视化分析--节假日客流量分析
- 分析Hibernate的缓存机制
- 数据结构实验之串二:字符串匹配(查看一个字符串是否为另一个字符串的子串)
- [Lintcode]爬梯子
- UVA679DroppingBalls
- 给初学者的RxJava2.0教程(一)