密码类型默认取消屏蔽

时间:2018-08-01 10:37:29

标签: android android-layout

我正在使用具有密码类型的Android TextInputLayout。我看到一个遮罩/取消遮罩图标。如何将文本默认设置为不带遮罩?

        <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textInputLayout"
        app:passwordToggleEnabled="true"
        app:theme="@style/EditTextMaterialTheme">

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:inputType="textVisiblePassword"
            android:maxLines="1"
            android:singleLine="true"
            android:text=""
            android:textColorLink="@android:color/darker_gray" />

    </android.support.design.widget.TextInputLayout>

2 个答案:

答案 0 :(得分:0)

   // When you define your edittext in oncreate method, that time put below line    
     youreditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

答案 1 :(得分:0)

您的XML应该是这样

<android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:passwordToggleEnabled="true"
        app:theme="@style/EditTextMaterialTheme">

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Password"
            android:inputType="textPassword"
            android:maxLines="1"
            android:singleLine="true"
            android:text=""
            android:textColorLink="@android:color/darker_gray" />

    </android.support.design.widget.TextInputLayout>

然后在您的Java类中编写以下行

TextInputLayout textInputLayout = findViewById(R.id.textInputLayout2);
        textInputLayout.passwordVisibilityToggleRequested(true);