一行代码监听EditText变化改变Button可否点击

来源:互联网 发布:win10 优化工具 编辑:程序博客网 时间:2024/05/10 20:21

一行代码监听EditText变化改变Button可否点击

  • 我们要实现的效果为在未输入时显示默认的颜色且无法点击,在EditText输入后改变按钮颜色且可点击,点击后改变颜色。

    • 主要思路为:首先创建按钮变化的drawable文件,这个我就不多说了,直接上代码。
  • button_disabled.xml

 <?xml version="1.0" encoding="utf-8"?>    <shape xmlns:android="http://schemas.android.com/apk/res/android"        android:shape="rectangle">        <solid android:color="#911b77c6" />        <corners android:radius="5dp" />        <stroke            android:width="1dp"            android:color="#910062b8" />    </shape>
  • button_normal.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid android:color="#1b77c6" />    <corners android:radius="5dp" />    <stroke        android:width="1dp"        android:color="#0062b8" /></shape>
  • button_pressed.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <solid android:color="#0062b8" />    <corners android:radius="5dp" /></shape>
  • button_select.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_window_focused="false"        android:drawable="@drawable/button_normal" />    <item android:state_focused="false" android:state_pressed="true"        android:drawable="@drawable/button_pressed" /></selector>
  • 按钮的布局文件:
<EditText                    android:id="@+id/et_b"                    android:hint="0.00"                    android:background="@null"                    android:inputType="number"                    android:textColorHint="#cdcfd1"                    android:textColor="@color/back_7"                    android:textSize="20sp"                    android:layout_marginLeft="16dp"                    android:layout_width="match_parent"                    android:layout_height="wrap_content" /><Button            android:id="@+id/tv_btn"            android:layout_width="match_parent"            android:layout_height="48dp"            android:layout_marginLeft="16dp"            android:layout_marginRight="16dp"            android:background="@drawable/button_disabled"            android:gravity="center"            android:text="确定"            android:enabled="false"            android:textColor="@color/white"      android:textSize="18sp" />
  • 现在我们就上主要的代码:
 public class TextChangeUtils implements TextWatcher {    private TextView btn;    private List<EditText> mList = new ArrayList<>();    public TextChangeUtils(EditText editText, TextView btn) {        mList.add(editText);        this.btn = btn;    }    public TextChangeUtils(List<EditText> mList, TextView btn) {        this.mList = mList;        this.btn = btn;    }    @Override    public void afterTextChanged(Editable arg0) {    }    @Override    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,                                  int arg3) {    }    @Override    public void onTextChanged(CharSequence cs, int start, int before,                              int count) {        for (int i = 0; i < mList.size(); i++) {            if (mList.get(i).getText().length() > 0) {                if (i + 1 == mList.size()) {                    btn.setBackgroundResource(R.drawable.button_select);                    btn.setEnabled(true);                }            } else {                btn.setBackgroundResource(R.drawable.button_disabled);                btn.setEnabled(false);                break;            }        }    }}
  • 上面的代码很简单就不多说了,我定义了两个方法供调用,一个是判断单个EditText是否输入,一个是传入一个集合的EditText。现在我们来看一下如何调用。
etB.addTextChangedListener(new TextChangeUtils(etB, tvBtn));

你只需要找到你的控件传入就可实现了,是不是很简单,方法还有很多可改进的地方,望广大袁友改进。。。

0 0