登录输入框限制长度改变按钮颜色

MainActivity

//登录验证
    private void init() {
        EditText pass_phone = findViewById(R.id.pass);   //手机号
        final Button commit = findViewById(R.id.commit);
        pass_phone.addTextChangedListener(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) {
                 //按钮可以点击 改变背景色
                commit.setEnabled((s.length() == 11));
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

activity_main.xml

 <EditText
           android:id="@+id/pass"
           android:layout_width="0dp"
           android:layout_height="50dp"
           android:hint=" 请输入手机号"
           android:textSize="25sp"
           android:numeric="integer"
           android:maxLength="11"
           app:layout_constraintLeft_toRightOf="@+id/text1"
           app:layout_constraintRight_toRightOf="parent"
           app:layout_constraintTop_toBottomOf="@+id/phone"
           />

    <Button
        android:id="@+id/commit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="下一步"
        android:background="@drawable/selector"
        android:textSize="25sp"
        app:layout_constraintTop_toBottomOf="@+id/pass"
        android:layout_marginTop="20dp"
        />

selecter.xml选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_enabled="false">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="#b3e9f3"/>
            <corners android:radius="15dp"/>

        </shape>
    </item>

    <item android:state_enabled="true">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="#b3e04d"/>
            <corners android:radius="15dp"/>

        </shape>
    </item>

</selector>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值