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


当前回答

添加这个方法:

fun EditText.revertTransformation() {
    transformationMethod = when(transformationMethod) {
        is PasswordTransformationMethod -> SingleLineTransformationMethod.getInstance()
        else -> PasswordTransformationMethod.getInstance()
    }
}

调用它将在输入类型状态之间切换(您可以将Single-Line转换更改为您喜欢的)。使用的例子:

editText.revertTransformation()

其他回答

根据这个来源,如果你已经将你的项目迁移到AndroidX,那么你可以替换

编译”com.android.support:设计:24.2.0”

实现“com.google.android.material:材料:1.0.0”

然后你所要做的就是把下面的代码放到你的布局文件中:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:passwordToggleEnabled="true"
    android:hint="@string/hint_text">

  <com.google.android.material.textfield.TextInputEditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>

</com.google.android.material.textfield.TextInputLayout>

关于材质TextInputLayout的更多信息可以在这里找到。

对于这个源码,建议从Android支持库迁移到AndroidX:

AndroidX is the open-source project that the Android team uses to develop, test, package, version and release libraries within Jetpack. AndroidX is a major improvement to the original Android Support Library. Like the Support Library, AndroidX ships separately from the Android OS and provides backwards-compatibility across Android releases. AndroidX fully replaces the Support Library by providing feature parity and new libraries. In addition AndroidX includes the following features: All packages in AndroidX live in a consistent namespace starting with the string androidx. The Support Library packages have been mapped into corresponding androidx.* packages. For a full mapping of all the old classes and build artifacts to the new ones, see the Package Refactoring page. Unlike the Support Library, AndroidX packages are separately maintained and updated. The androidx packages use strict Semantic Versioning starting with version 1.0.0. You can update AndroidX libraries in your project independently. All new Support Library development will occur in the AndroidX library. This includes maintenance of the original Support Library artifacts and introduction of new Jetpack components.

显示:

editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

隐藏:

editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

在这些之后,光标被重置,所以:

editText.setSelection(editText.length());

试试这个:

首先定义一个全局标志,如下所示:

private boolean isShowPassword = false;

并设置监听器处理点击显示和隐藏密码按钮:

imgPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isShowPassword) {
                    etPassword.setTransformationMethod(new PasswordTransformationMethod());
                    imgPassword.setImageDrawable(getResources().getDrawable(R.drawable.ic_eye_hide));
                    isShowPassword = false;
                }else{
                    etPassword.setTransformationMethod(null);
                    imgPassword.setImageDrawable(getResources().getDrawable(R.drawable.ic_eye_show));
                    isShowPassword = true;
                }
            }
        });

添加这个方法:

fun EditText.revertTransformation() {
    transformationMethod = when(transformationMethod) {
        is PasswordTransformationMethod -> SingleLineTransformationMethod.getInstance()
        else -> PasswordTransformationMethod.getInstance()
    }
}

调用它将在输入类型状态之间切换(您可以将Single-Line转换更改为您喜欢的)。使用的例子:

editText.revertTransformation()

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

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

然后更改XML文件中的EditText。

来自:< EditText

: < com.scottyab.showhidepasswordedittext.ShowHidePasswordEditText

这是所有。

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