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


当前回答

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>

结果:

其他回答

您可以动态地更改TextView的属性。如果你将XML属性android:password设置为真,视图将显示点,如果你设置为假,文本显示。

使用setTransformationMethod方法,您应该能够从代码中更改此属性。(免责声明:我没有测试过该方法在视图显示后是否仍然有效。如果你遇到问题,请给我留下评论,让我知道。)

完整的示例代码将是

yourTextView.setTransformationMethod(new PasswordTransformationMethod());

隐藏密码。要显示密码,您可以设置一个现有的转换方法,或者实现一个空的TransformationMethod,它对输入文本不做任何操作。

yourTextView.setTransformationMethod(new DoNothingTransformation());

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

我使用了一个OnClickListener(),它与我想用作toogle的按钮相关联。

private EditText email_et, contraseña_et;
protected void onCreate(Bundle savedInstanceState) {
....
contraseña_et = (EditText) findViewById(R.id.contraseña_et);
....
vercontra_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int inputType = contraseña_et.getInputType();
            if (inputType == 129){
                contraseña_et.setInputType(1);
            } else {
                contraseña_et.setInputType(129);
            }
            contraseña_et.setSelection(contraseña_et.getText().length());
        }
    });

阅读文档,int值似乎是不同的,所以我调试找到正确的值,它的工作棒极了,这是一个更容易一点的方式。

[顺便说一下,Contraseña是西班牙语的密码]

首先,这是屏幕加载图像矢量资产可见性

点击它将改变这个图像的可见性关闭

以上密码开关代码(xml代码)

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/laypass"
    android:layout_width="330dp"
    android:layout_height="50dp"
    android:layout_marginTop="24dp"
    app:layout_constraintEnd_toEndOf="@+id/editText3"
    app:layout_constraintStart_toStartOf="@+id/editText3"
    app:layout_constraintTop_toBottomOf="@+id/editText3">

    <EditText
        android:id="@+id/edit_password"
        style="@style/EditTextTheme"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/round"
        android:drawableLeft="@drawable/ic_password"
        android:drawablePadding="10dp"
        android:ems="10"
        android:hint="Password"
        android:inputType="textPassword"
        android:paddingLeft="10dp"
        android:paddingRight="15dp"
        android:textColor="@color/cyan92a6"
        android:textColorHint="@color/cyan92a6"
        android:textCursorDrawable="@null"
        android:textSize="18sp"
        />

    <ImageView
        android:id="@+id/show_pass_btn"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="8dp"
        android:alpha=".5"
        android:onClick="ShowHidePass"
        android:padding="5dp"
        android:src="@drawable/ic_visibility"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/laypass"
        app:layout_constraintTop_toTopOf="@+id/edit_password" /> 
 </androidx.constraintlayout.widget.ConstraintLayout>

按钮操作的Java代码

public void ShowHidePass(View view) {

    if(view.getId()==R.id.show_pass_btn){
        if(edit_password.getTransformationMethod().equals(PasswordTransformationMethod.getInstance())){
            ((ImageView)(view)).setImageResource(R.drawable.ic_visibility_off);
            //Show Password
            edit_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
        else{
            ((ImageView)(view)).setImageResource(R.drawable.ic_visibility);
            //Hide Password
            edit_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
    }
}

这是我的工作。这绝对对你有帮助

showpass.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(!isChecked){

                    // show password
                    password_login.setTransformationMethod(PasswordTransformationMethod.getInstance());

                    Log.i("checker", "true");
                }

                else{
                    Log.i("checker", "false");

                     // hide password
    password_login.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }

            }
        });