有没有一个聪明的方法让用户在隐藏和查看密码之间切换在一个android EditText? 许多基于PC的应用程序都允许用户这样做。


当前回答

你可以使用下面的代码显示/隐藏密码:

XML代码:

<EditText
        android:id="@+id/etPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="21dp"
        android:layout_marginTop="14dp"
        android:ems="10"
        android:inputType="textPassword" >
        <requestFocus />
    </EditText>
    <CheckBox
        android:id="@+id/cbShowPwd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/etPassword"
        android:layout_below="@+id/etPassword"
        android:text="@string/show_pwd" />

JAVA代码:

EditText mEtPwd;
CheckBox mCbShowPwd;


mEtPwd = (EditText) findViewById(R.id.etPassword);
mCbShowPwd = (CheckBox) findViewById(R.id.cbShowPwd);

mCbShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // checkbox status is changed from uncheck to checked.
        if (!isChecked) {
            // show password
            mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            // hide password
            mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }
});

其他回答

要显示圆点而不是密码,请设置PasswordTransformationMethod:

yourEditText.setTransformationMethod(new PasswordTransformationMethod());

当然,您可以在XML布局中的edittext元素中默认设置此选项

android:password

要重新显示可读密码,只需传递null作为转换方法:

yourEditText.setTransformationMethod(null);

1 -创建一个选择器文件"show_password_selector.xml"

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pwd_hide"
          android:state_selected="true"/>
    <item android:drawable="@drawable/pwd_show"
          android:state_selected="false" />
 </selector>

2 -将“show_password_selector”文件放入imageview。

<ImageView
    android:id="@+id/iv_pwd"
    android:layout_width="@dimen/_35sdp"
    android:layout_height="@dimen/_25sdp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="@dimen/_15sdp"
    android:src="@drawable/show_password_selector" />

3 -把下面的代码在java文件。

iv_new_pwd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (iv_new_pwd.isSelected()) {
            iv_new_pwd.setSelected(false);
            Log.d("mytag", "in case 1");
            edt_new_pwd.setInputType(InputType.TYPE_CLASS_TEXT);
        } else {
            Log.d("mytag", "in case 1");
            iv_new_pwd.setSelected(true);
            edt_new_pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }
    }
});

非常简单的形式:

private fun updatePasswordVisibility(editText: AppCompatEditText) {
        if (editText.transformationMethod is PasswordTransformationMethod) {
            editText.transformationMethod = null
        } else {
            editText.transformationMethod = PasswordTransformationMethod()
        }
        editText.setSelection(editText.length())
    }

希望能有所帮助。

你试过setTransformationMethod吗?它继承自TextView,并希望TransformationMethod作为参数。

你可以在这里找到更多关于TransformationMethods的信息。

它还有一些很酷的功能,比如字符替换。

我所做的是

创建一个编辑文本视图和一个普通文本视图 通过使用约束布局使它们相互重叠(就像Facebook应用登录屏幕一样) 将onClickListener附加到普通文本视图,以便它相应地改变编辑文本视图的输入类型(可见/不可见)

你可以看看这个视频,了解更详细的步骤和解释https://youtu.be/md3eVaRzdIM

希望能有所帮助。