Android 密码框的眼睛按下显示抬起隐藏

按下右边的眼睛图标显示密码,手指抬起密码隐藏在这里插入图片描述
xml文件

  <RelativeLayout
        android:layout_marginTop="@dimen/dp_5"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_22">
        <EditText
            android:id="@+id/et_dialog_mm"
            android:layout_width="match_parent"
            android:layout_height= "@dimen/dp_22"
            style="@style/setEditText"
            android:textSize="@dimen/dp_11"
            android:inputType="textPassword"
            android:hint="请输入原始密码"
            />
        <ImageView
            android:id="@+id/iv_eye1"
            android:layout_width="@dimen/dp_20"
            android:layout_height="@dimen/dp_22"
            android:layout_alignParentRight="true"
            android:src="@drawable/selector_icon_eye"/>
    </RelativeLayout>

java类

//定义控件
  public EditText etdialogmm;
  private ImageView iv_eye1;
······
//绑定控件
 iv_eye1=findViewById(R.id.iv_eye1);
 etdialogmm = findViewById(R.id.et_dialog_mm);
 iv_eye1.setOnTouchListener(new myOnTouchListener());
 ······
 //眼睛图标的触摸事件
  private class myOnTouchListener implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            if (v.getId() == R.id.iv_eye1) {
                switch (action) {
                    case MotionEvent.ACTION_DOWN://按下(按下动作)
                        iv_eye1.setSelected(true);
                        //密码可见
                        etdialogmm.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        break;
                    case MotionEvent.ACTION_UP://抬起(抬起动作)
                        iv_eye1.setSelected(false);
                        //密码不可见
                        etdialogmm.setTransformationMethod(PasswordTransformationMethod.getInstance());
                        break;
                }
            }
            return true;
        }
    }

selector_icon_eye.xml

//眼睛图标的样式
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/see1" android:state_selected="false" />
    <item android:drawable="@mipmap/see" android:state_selected="true" />
</selector>
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值