Android 如何在隐藏和查看密码之间切换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3685790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:21:13  来源:igfitidea点击:

How to switch between hide and view password

androidpasswords

提问by Hymannad

Is there a clever way to let the user switch between hide and view password in an android EditText? A number of PC based apps let the user do this.

是否有一种巧妙的方法可以让用户在 android EditText 中在隐藏和查看密码之间切换?许多基于 PC 的应用程序允许用户执行此操作。

采纳答案by Janusz

You can dynamically change the attributes of a TextView. If you would set the XML Atrribute android:passwordto true the view would show dots if you set it to false the text is shown.

您可以动态更改 TextView 的属性。如果将 XML 属性设置android:password为 true,则视图将显示点,如果将其设置为 false,则显示文本。

With the method setTransformationMethodyou should be able to change this attributes from code. (Disclaimer: I have not tested if the method still works after the view is displayed. If you encounter problems with that leave me a comment for me to know.)

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

The full sample code would be

完整的示例代码将是

yourTextView.setTransformationMethod(new PasswordTransformationMethod());

to hide the password. To show the password you could set one of the existing transformation methods or implement an empty TransformationMethodthat does nothing with the input text.

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

yourTextView.setTransformationMethod(new DoNothingTransformation());

回答by mmBs

It is really easy to achieve since the Support Library v24.2.0.

从支持库 v24.2.0 开始,这真的很容易实现。

What you need to do is just:

您需要做的只是:

  1. Add the design library to your dependecies

    dependencies {
         compile "com.android.support:design:24.2.0"
    }
    
  2. Use TextInputEditTextin conjunction with TextInputLayout

    <android.support.design.widget.TextInputLayout
        android:id="@+id/etPasswordLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true"
        android:layout_marginBottom="@dimen/login_spacing_bottom">
    
        <android.support.design.widget.TextInputEditText
            android:id="@+id/etPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/fragment_login_password_hint"
            android:inputType="textPassword"/>
    </android.support.design.widget.TextInputLayout>
    
  1. 将设计库添加到您的依赖项

    dependencies {
         compile "com.android.support:design:24.2.0"
    }
    
  2. 使用TextInputEditText会同TextInputLayout

    <android.support.design.widget.TextInputLayout
        android:id="@+id/etPasswordLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true"
        android:layout_marginBottom="@dimen/login_spacing_bottom">
    
        <android.support.design.widget.TextInputEditText
            android:id="@+id/etPassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/fragment_login_password_hint"
            android:inputType="textPassword"/>
    </android.support.design.widget.TextInputLayout>
    

The passwordToggleEnabledattribute will do the job!

passwordToggleEnabled属性将完成工作!

  1. In your root layout don't forget to add xmlns:app="http://schemas.android.com/apk/res-auto"

  2. You can customize your password toggle by using:

  1. 在你的根布局不要忘记添加 xmlns:app="http://schemas.android.com/apk/res-auto"

  2. 您可以使用以下方法自定义密码切换:

app:passwordToggleDrawable- Drawable to use as the password input visibility toggle icon.
app:passwordToggleTint- Icon to use for the password input visibility toggle.
app:passwordToggleTintMode- Blending mode used to apply the background tint.

app:passwordToggleDrawable- 可绘制用作密码输入可见性切换图标。
app:passwordToggleTint- 用于密码输入可见性切换的图标。
app:passwordToggleTintMode- 用于应用背景色调的混合模式。

More details in TextInputLayout documentation.

TextInputLayout 文档中的更多详细信息。

enter image description here

在此处输入图片说明

For AndroidX

对于 AndroidX

  • Replace android.support.design.widget.TextInputLayoutwith com.google.android.material.textfield.TextInputLayout

  • Replace android.support.design.widget.TextInputEditTextwith com.google.android.material.textfield.TextInputEditText

  • 替换android.support.design.widget.TextInputLayoutcom.google.android.material.textfield.TextInputLayout

  • 替换android.support.design.widget.TextInputEditTextcom.google.android.material.textfield.TextInputEditText

回答by Qlimax

To show the dots instead of the password set the PasswordTransformationMethod:

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

yourEditText.setTransformationMethod(new PasswordTransformationMethod());

of course you can set this by default in your edittext element in the xml layout with

当然,您可以在 xml 布局中的 edittext 元素中默认设置它

android:password

To re-show the readable password, just pass null as transformation method:

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

yourEditText.setTransformationMethod(null);

回答by Matt Logan

To show:

显示:

editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

To hide:

隐藏:

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

After each of these the cursor is reset, so:

在每一个之后,光标都被重置,所以:

editText.setSelection(editText.length());

回答by Tabish khan

You can use app:passwordToggleEnabled="true"

您可以使用 app:passwordToggleEnabled="true"

here is example given below

这是下面给出的示例

<android.support.design.widget.TextInputLayout
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true"
        android:textColorHint="@color/colorhint"
        android:textColor="@color/colortext">

回答by Praveena

Use checkbox and change the input type accordingly.

使用复选框并相应地更改输入类型。

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    int start,end;
    Log.i("inside checkbox chnge",""+isChecked);
    if(!isChecked){
        start=passWordEditText.getSelectionStart();
        end=passWordEditText.getSelectionEnd();
        passWordEditText.setTransformationMethod(new PasswordTransformationMethod());;
        passWordEditText.setSelection(start,end);
    }else{
        start=passWordEditText.getSelectionStart();
        end=passWordEditText.getSelectionEnd();
        passWordEditText.setTransformationMethod(null);
        passWordEditText.setSelection(start,end);
    }
}

回答by Vladyslav Ulianytskyi

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

回答by sujith s

It's work for me.This will help you definitely

这对我有用。这肯定会帮助你

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

            }
        });

回答by Mina Fawzy

I feel I want answer this question even there some good answers ,

我觉得我想回答这个问题,即使有一些好的答案,

according to documentation TransformationMethoddo our mission

根据文档TransformationMethod做我们的使命

TransformationMethod

TextView uses TransformationMethods to do things like replacing the characters of passwords with dots, or keeping the newline characters from causing line breaks in single-line text fields.

变换方法

TextView 使用 TransformationMethods 来做一些事情,比如用点替换密码字符,或者防止换行符导致单行文本字段中的换行符。

NoticeI use butter knife, but its the same if user check show password

注意我用的是黄油刀,但如果用户检查显示密码也是一样的

@OnCheckedChanged(R.id.showpass)
    public void onChecked(boolean checked){
        if(checked){
            et_password.setTransformationMethod(null);
        }else {
            et_password.setTransformationMethod(new PasswordTransformationMethod());

        }
       // cursor reset his position so we need set position to the end of text
        et_password.setSelection(et_password.getText().length());
    }

回答by david m lee

I'm able to add the ShowPassword / HidePassword code with just a few lines, self-contained in a block:

我只需几行就可以添加 ShowPassword / HidePassword 代码,这些代码独立于一个块中:

protected void onCreate(Bundle savedInstanceState) {
    ...
    etPassword = (EditText)findViewById(R.id.password);
    etPassword.setTransformationMethod(new PasswordTransformationMethod()); // Hide password initially

    checkBoxShowPwd = (CheckBox)findViewById(R.id.checkBoxShowPwd);
    checkBoxShowPwd.setText(getString(R.string.label_show_password)); // Hide initially, but prompting "Show Password"
    checkBoxShowPwd.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            if (isChecked) {
                etPassword.setTransformationMethod(null); // Show password when box checked
                checkBoxShowPwd.setText(getString(R.string.label_hide_password)); // Prompting "Hide Password"
            } else {
                etPassword.setTransformationMethod(new PasswordTransformationMethod()); // Hide password when box not checked
                checkBoxShowPwd.setText(getString(R.string.label_show_password)); // Prompting "Show Password"
            }
        }
    } );
    ...