Android:EditText设置密码可见与不可见

 急的看这里,直接在点击事件中设置EditText的密码可见与不可见属性:

et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); //密码可见
et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());//密码不可见

 正文:

(参考https://blog.csdn.net/song_74110/article/details/69240506

刚开始我是修改EditText的inputType,这个就是EditText的输入模式。我之前也是想修改这个inputType的,这个在xml里面修改可以,代码是这样的:android:inputType=”textPassword”(密码不可见)android:inputType=”textVisiblePassword”( 可见密码) 有两种模式。但是想在java代码里面动态修改貌似改显示与不可见,貌似只能从可见模式转为可见模式,但是转不回去。若有大神可以从可见模式转为不可见模式的告诉我哈,我也学习学习-_-代码如下:

  • 动态修改inputType
    //从密码不可见模式变为密码可见模式(行得通)
    et_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
    //从密码可见模式变为密码不可见模式(行不通)
    et_password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

我也阅读InputType里面的源码,也没发现inputType从密码不可见模式可以转为可见模式
inputType里面的部分源码说明如下如下:

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering a password, which should
     * be visible to the user.
     */
    public static final int TYPE_TEXT_VARIATION_VISIBLE_PASSWORD = 0x00000090;

    /**
     * Variation of {@link #TYPE_CLASS_TEXT}: entering a password.
     */
    public static final int TYPE_TEXT_VARIATION_PASSWORD = 0x00000080;

 

解决方法 :动态修改TransformationMethod

    //从密码不可见模式变为密码可见模式
    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    //从密码可见模式变为密码不可见模式
    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());

下面来一个小demo

效果图:
这里写图片描述

这里写图片描述

这里写图片描述

  • xml布局文件 一个线性布局里面左右各方一个ImageButton,中间放一个EditText,图片自己找喔
    <!--请输入密码-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/login_background"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="20dp"
            android:layout_height="25dp"
            android:layout_marginLeft="10dp"
            android:src="@drawable/icon_coupon" />

        <EditText
            android:id="@+id/et_password"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:hint="@string/input_password"
            android:imeOptions="actionDone"
            android:inputType="textPassword"
            android:paddingLeft="5dp"
            android:textColor="@color/black"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/iv_eye"
            android:layout_width="20dp"
            android:layout_height="25dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:src="@drawable/icon_eye" />
    </LinearLayout>
  • 上面的LinearLayout的background com_btn_background.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <!--描边-->
    <stroke
        android:width="1.0dp"
        android:color="#ededed" />

    <!--填充颜色-->
    <solid android:color="#ffffff" />

</shape>
  • LinearLayout右边的图片有一个选择器selector icon_eye.xml如下(图片自己找哈):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/icon_eye_closs" android:state_selected="false" />
    <item android:drawable="@drawable/icon_eye_show" android:state_selected="true" />

</selector>
  • java代码如下:

    private EditText et_password;
    private ImageView ivEye;
    private boolean isOpenEye = false;
    //绑定id
    et_password = (EditText) v.findViewById(R.id.et_password);
    ivEye = (ImageView) v.findViewById(R.id.iv_eye);

    //密码是否可见
    ivEye.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isOpenEye) {
                    ivEye.setSelected(true);
                    isOpenEye = true;
                    //密码可见
                    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    ivEye.setSelected(false);
                    isOpenEye = false;
                    //密码不可见
                    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });

猜你喜欢

转载自blog.csdn.net/LucasXu01/article/details/83025441
今日推荐