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


当前回答

我也遇到过同样的问题,而且它很容易实现。

你所要做的就是在一个(com.google.android.material.textfield.TextInputLayout)和添加(app:passwordToggleEnabled="true")中包装你的EditText字段。

这将在EditText字段中显示眼睛,当您单击它时,密码将出现并在再次单击时消失。

<com.google.android.material.textfield.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:textColorHint="#B9B8B8"
                app:passwordToggleEnabled="true">

                <EditText
                    android:id="@+id/register_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="24dp"
                    android:layout_marginRight="44dp"
                    android:backgroundTint="#BEBEBE"
                    android:hint="Password"
                    android:inputType="textPassword"
                    android:padding="16dp"
                    android:textSize="18sp" />
            </com.google.android.material.textfield.TextInputLayout>

其他回答

if (inputPassword.getTransformationMethod() == PasswordTransformationMethod.getInstance()) {
 //password is visible
                inputPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }
else if(inputPassword.getTransformationMethod() == HideReturnsTransformationMethod.getInstance()) {
 //password is hidden
                inputPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }

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

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());
        }
    }
});
you can use this

private fun showPasswordSwitch(checked: Boolean) {

    registerBinding.registerActivityEditTextPassword.also {
        // Password hide-show
        it.inputType =
            if (checked) TEXT_PASSWORD_SHOW else TEXT_PASSWORD_HIDE
           
    }
}
const val TEXT_PASSWORD_SHOW = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

const val TEXT_PASSWORD_HIDE = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD

使用下面的代码

    val hidePasswordMethod = PasswordTransformationMethod()
    showOrHidePasswordButton.setOnClickListener {
        passwordEditText.apply {
            transformationMethod =
                if (transformationMethod is PasswordTransformationMethod) 
                   null //shows password
               else 
                   hidePasswordMethod //hides password
        }
    }

并确保你把它添加到你的密码编辑文本在布局

    android:inputType="textPassword"

如果你想要一个简单的解决方案,你可以使用这个扩展的Android的EditText去这个链接了解更多信息:https://github.com/scottyab/showhidepasswordedittext

将此添加到build.gradle中: 实现“com.github.scottyab: showhidepasswordedittext: 0.8”

然后更改XML文件中的EditText。

来自:< EditText

: < com.scottyab.showhidepasswordedittext.ShowHidePasswordEditText

这是所有。

注意:在XML设计中无法看到它,请尝试在模拟器或物理设备中运行它。