Android EditText 密码隐藏或可见

Android EditText 密码隐藏或可见

  • 问题
  • 解决
  • 完事

问题

很常见的需求,点击一个按钮,隐藏或可见输入密码。

解决

布局:

<EditText
   android:id="@+id/edt_password"
   style="@style/Base.Widget.MaterialComponents.TextInputEditText"
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:layout_marginLeft="@dimen/def_padding_left_right"
   android:background="@null"
   android:hint="@string/voice_link_network_wifi_password"
   android:inputType="textPassword"
   android:singleLine="true"
   android:textColor="@color/text_normal_color"
   android:textSize="@dimen/def_text_size" />

代码:

    /**
     * 显示密码
     * @param show
     */
    public void setSeePassword(boolean show) {
        imgSee.setSelected(show);
        edtPassword.setTransformationMethod(show?
                HideReturnsTransformationMethod.getInstance() :
                PasswordTransformationMethod.getInstance());
    }

显示密码使用:

edtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());

隐藏密码使用:

edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());

完事

你可能感兴趣的:(Android知识点,Android,edittext)