Android materialdesign TextInputLayout替换眼睛图标和解决眼睛图标不显示的问题

TextInputLayout自带的那个眼睛点击后是只能加深背景颜色,老板看了觉得不满意就让我修改 OTZ,但是我又不想放弃TextInputLayout显示文字的酷炫效果,所以就有了下文。
(1)用自己的眼睛图标替换系统的
图片一,这是默认的效果
于是我百度了一下如何隐藏TextInputLayout眼睛图标,于是看到了这个属性
TextInputLayout设置眼睛图标不显示app:passwordToggleEnabled=”false”,然后加上自己的样式实现了这个效果。
图片二,实现了点击眼睛显示密码
实现方式如下
XML代码
<android.support.design.widget.TextInputLayout
android:id="@+id/passwordWrapper"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_below="@id/usernameWrapper"
app:passwordToggleEnabled="false">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:textSize="14sp"
android:inputType="numberPassword"/>
</android.support.design.widget.TextInputLayout>
<CheckBox
android:id="@+id/showpassword"
android:checked="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:button="@drawable/show_password"/>

show_password样式
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/show" android:state_checked="true"/>
<item android:drawable="@drawable/conceal" android:state_checked="false"/>
</selector>

Activity代码

/**
* 显示或者隐藏密码
*/
showpassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
passwordWrapper.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
}else {
passwordWrapper.getEditText().
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
}
});

这样就实现了想要的效果,但是两个edittext的宽度不一样感觉不好看,APP里还有几个需要修改,重复的代码要复制好几份,所以我就想看下TextInputLayout里的属性看能不能修改下眼睛图标的样式,于是就被我找到了这货app:passwordToggleDrawable=”@drawable/show_password”,它是用来替换眼睛图标的样式的,于是加上自己的样式再稍微修改了下图标的大小就实现了下面的效果完美贴合edittext。
默认显示的效果
明文显示密码的效果

(2)解决眼睛图标不显示的问题
最近发现眼睛图标突然消失不见了,以为是系统问题,结果用不同产商的手机测试了还真是系统问题,猜想可能是被自动隐藏了,解决办法也挺简单在TextInputLayout 控件的xml代码中加入以下属性,手动将眼睛图标显示出来。

app:passwordToggleEnabled=”true”

 总结:以后遇到问题之前先去尝试自己解决,不要直接百度去找答案,也许这个问题其实并没有那么难!
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值