Android:EditText set password visible or invisible

Posted by Moron on Mon, 16 Dec 2019 22:23:26 +0100

Urgently, set the password visible and invisible properties of EditText in the click event:

et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); //Password not visible
et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());//Password visible

 

Body:

(Ref https://blog.csdn.net/song_74110/article/details/69240506)

At first, I modified the inputType of EditText, which is the input mode of EditText. I also want to modify the inputType before. This can be modified in xml. The code is as follows: android:inputType = "textPassword" (password is invisible) android:inputType = "textVisiblePassword" (visible password) has two modes. But if you want to change the display and invisibility in java code dynamically, it seems that you can only change from visible mode to visible mode, but you can't go back. If there is a God who can change from visible mode to invisible mode, tell me ha ha, I also learn to learn -? Code is as follows:

  • Dynamically modify inputType
    //Change from password invisible mode to password visible mode (OK)
    et_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
    //Change from password visible mode to password invisible mode (does not work)
    et_password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

I also read the source code in inputtype, and I didn't find that inputtype can be changed from invisible mode to visible mode
Some source codes in inputType are as follows:

    /**
     * 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;

 

Solution: dynamically modify transformation method

    //Change from password invisible mode to password visible mode
    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
    //Change from password visible mode to password invisible mode
    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());

Here's a small demo

design sketch:

  • xml layout file: a linear layout with an ImageButton on the left and right, an EditText in the middle, and the picture can be found by yourself
    <!--Please input a password-->
    <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>
  • The background com? BTN? Background.xml of LinearLayout above is as follows:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

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

    <!--fill color-->
    <solid android:color="#ffffff" />

</shape>
  • The picture on the right of LinearLayout has a selector icon [eye. XML as follows (the picture can be found by itself):
<?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>
  • The java code is as follows:
    private EditText et_password;
    private ImageView ivEye;
    private boolean isOpenEye = false;
    //Binding id
    et_password = (EditText) v.findViewById(R.id.et_password);
    ivEye = (ImageView) v.findViewById(R.id.iv_eye);

    //Is the password visible
    ivEye.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isOpenEye) {
                    ivEye.setSelected(true);
                    isOpenEye = true;
                    //Password visible
                    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    ivEye.setSelected(false);
                    isOpenEye = false;
                    //Password not visible
                    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });

 

Topics: Android xml Java encoding