Android 密码框的眼睛按下显示抬起隐藏
程序员文章站
2022-07-05 21:35:45
按下右边的眼睛图标显示密码,手指抬起密码隐藏xml文件
按下右边的眼睛图标显示密码,手指抬起密码隐藏
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>
本文地址:https://blog.csdn.net/weixin_45407364/article/details/108704634