Show the password with EditText

2020-05-15 07:40发布

I use an EditText to enter password. And a CheckBox to show password or not. Below function is the part:

public void ShowPassword() {
    if (cb.isChecked()) {
        password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
    } else {
        password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
    }
}

When it checked, it show password. But when it not checked, it does show stars. How to modify it to show star while the cb is not checked?

12条回答
家丑人穷心不美
2楼-- · 2020-05-15 08:20

use app:passwordToggleEnabled = true; available from Android support library version 24.2.0.

  <android.support.design.widget.TextInputLayout
    android:id="@+id/input_layout_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="gone"
    app:passwordToggleEnabled="true"
    tools:visibility="visible"
    android:layout_marginBottom="@dimen/space_medium">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/input_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:imeOptions="actionDone"
        android:imeActionLabel="@string/btn_sign_in"
        android:hint="@string/hint_password" />

</android.support.design.widget.TextInputLayout>
查看更多
ゆ 、 Hurt°
3楼-- · 2020-05-15 08:21

I don't know exactly the specifics, but this code should work:

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(!isChecked) {
                    password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
                } else {
                    password.setInputType(129);
                }
            }
        });

129 is the input type set when setting android:inputType="textPassword"

edit:

as mentioned in @user370305's answer, 129 is the value of the bitwise or operation when you do

password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
查看更多
闹够了就滚
4楼-- · 2020-05-15 08:25

Call this method inside your OnCheckedChangedListener

 public static void toggleShowPassword(boolean show, EditText editText) {
    if (show)
        editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
    else
        editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    editText.setSelection(editText.length());
}

The EditText cursor resets its position after changing the InputType that's why we add the last line editText.setSelection(editText.length())

查看更多
看我几分像从前
5楼-- · 2020-05-15 08:26

I think you are using the wrong function. I make that way and work perfectly:

passwordEditView = (EditText) rootView.findViewById(R.id.password);
final CheckBox showPasswordCheckBox = (CheckBox) rootView.findViewById(R.id.checkbox);
showPasswordCheckBox.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (showPasswordCheckBox.isChecked()){
            passwordEditView.setTransformationMethod(null);
        }else{
            passwordEditView.setTransformationMethod(new PasswordTransformationMethod());
       }
    }
});
查看更多
【Aperson】
6楼-- · 2020-05-15 08:27
public void onCheckBox(View v2)
{



    CheckBox cb = (CheckBox)this.findViewById(R.id.pass_Check);
    EditText et1=(EditText)this.findViewById(R.id.edt_Pass);
        if(cb.isChecked())
        {
        et1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
        else 
        {
        et1.setInputType(129);
        }

}
查看更多
唯我独甜
7楼-- · 2020-05-15 08:30

on the off chance that you are using Xamarin (Visual Studio Mac as it's now called) you can achieve it this way (I used a Switch)

 /// <summary>
 /// Toggles the password.
 /// </summary>
 /// <param name="field">Field.</param>
 /// <param name="isChecked">If set to <c>true</c> is checked.</param>
 private void TogglePassword(TextView field, bool isChecked)
 {
      /// masks with password character
      if (isChecked)
      {
           field.TransformationMethod = new PasswordTransformationMethod();
      }
      /// unmasks password
      else
      {
           field.TransformationMethod = null;
      }

Then on your Switch .click do something like this

switch.Click += delegate {
            TogglePassword(textView, switch.Checked);
        };
查看更多
登录 后发表回答