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


当前回答

根据这个来源,如果你已经将你的项目迁移到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.

其他回答

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());
            }

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

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

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

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
private boolean isPasswordVisible;

private TextInputEditText firstEditText;

...

firstEditText = findViewById(R.id.et_first);

...

    private void togglePassVisability() {
    if (isPasswordVisible) {
        String pass = firstEditText.getText().toString();
        firstEditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        firstEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        firstEditText.setText(pass);
        firstEditText.setSelection(pass.length());           
    } else {
        String pass = firstEditText.getText().toString();
        firstEditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        firstEditText.setInputType(InputType.TYPE_CLASS_TEXT);
        firstEditText.setText(pass);
        firstEditText.setSelection(pass.length());
    }
    isPasswordVisible= !isPasswordVisible;
}

TextInputLayout的更新代码:

   <androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toTopOf="@+id/lg_forget_password"
    app:layout_constraintTop_toBottomOf="@+id/lg_name"
    app:cardElevation="@dimen/dp20"
    android:layout_marginStart="@dimen/dp20"
    app:cardCornerRadius="@dimen/dp10"
    android:layout_marginEnd="@dimen/dp20"
    android:id="@+id/textField">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="@android:color/transparent"
        app:boxStrokeWidth="0dp"
        app:boxStrokeWidthFocused="0dp"
        app:endIconMode="password_toggle"
        app:hintEnabled="false"
      >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/lg_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/input_bg"
            android:drawableStart="@drawable/ic_baseline_lock_24"
            android:drawablePadding="@dimen/dp10"
            android:fontFamily="@font/roboto_flex"
            android:hint="@string/password"
            android:inputType="textPassword"
            android:padding="@dimen/dp15"
            android:singleLine="true" />

    </com.google.android.material.textfield.TextInputLayout>
</androidx.cardview.widget.CardView>

结果: