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

发布时间:2024-07-12 15:01

按下右边的眼睛图标显示密码,手指抬起密码隐藏在这里插入图片描述
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>

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号