Android—密码的明密文转换

作者: MLGirl | 来源:发表于2017-08-02 14:50 被阅读60次

上述效果就是我们常见的关于密码文字的隐藏和显示效果,下面小编介绍一下此效果在 Android 中的实现。

1. 创建输入框和显示密码选框

(1)使用 EditText 控件创建一个输入密码的输入框

 <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:inputType="textPassword"
        android:id="@+id/pwd"
        />

(2)使用 checkBox 控件创建一个显示密码的选择框;

<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/pwd"
        android:layout_alignParentRight="true"
        android:text="显示密码"
        android:id="@+id/showPwd"/>

2.为 checkBox 添加监听事件

(1)初始化上步骤创建的 checkbox和 EditText控件

  private CheckBox showPwd;//checkbox
  private EditText pwd;//密码输入框

(2)为 checkBox 添加 setCheckedChangeListener()监听事件

 pwd=(EditText)findViewById(R.id.pwd);
 showPwd= (CheckBox) findViewById(R.id.showPwd);
 showPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
   @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       if (isChecked){
                      pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                        }
       else{
                      pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });

如果您感觉小编讲的 OK , 记得要分享哦,感谢一路有您,静等下期精彩!!!

相关文章

网友评论

    本文标题:Android—密码的明密文转换

    本文链接:https://www.haomeiwen.com/subject/pxzelxtx.html