一、代码实现
1.1 密码框布局
<!-- 密码框 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 编辑框 -->
<EditText
android:id="@+id/login_pwd"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_centerInParent="true"
android:hint="请输入密码"
android:inputType="textPassword"
android:maxLines="1"
android:paddingLeft="16dp"
android:textSize="16sp" />
<!-- 密码显示“眼睛”图标” -->
<ImageView
android:id="@+id/login_is_show_pwd"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_margin="10dp" />
</RelativeLayout>
</LinearLayout>
1.2 Java逻辑
1.2.1 完整代码
(1)在LoginActivity类中定义一个变量isHide,用于判断密码是否需要隐藏,默认为false,即不隐藏;
private boolean isHide = false; //输入框密码是否是隐藏,默认为false
(2)定义ImageView的点击响应函数isShowPassword();
private void isShowPassword() {
if(isHide == false) {
//R.mipmap.password_show是表示显示密码的“眼睛”图标
isPwdShowIv.setImageResource(R.mipmap.password_show);
//密文
HideReturnsTransformationMethod method1 = HideReturnsTransformationMethod.getInstance();
pwdEt.setTransformationMethod(method1);
isHide = true;
} else {
//R.mipmap.password_miss是表示隐藏密码的“眼睛+斜杠”图标
isPwdShowIv.setImageResource(R.mipmap.password_miss);
//密文
TransformationMethod method2 = PasswordTransformationMethod.getInstance();
pwdEt.setTransformationMethod(method2);
isHide = false;
}
//重置光标位置
int index = pwdEt.getText().toString().length();
pwdEt.setSelection(index) ;
}
注:其中的“显示密码”图标和“隐藏密码”图标可以在iconfont矢量库 中下载
(3)在LoginActivity类的onCreate()方法中设置 “显示密码” 按钮的监听器,代码如下:
isPwdShowIv = findViewById(R.id.login_is_show_pwd);
isPwdShowIv.setOnClickListener(this);
//设置ImageView的初始图标为“眼睛”,表示“显示密码”
isPwdShowIv.setImageResource(R.mipmap.password_miss);
(4)让LoginActivity类实现View.OnClickListener接口,并在onClick()函数中调用ImageView的点击响应函数;
//LoginActivity类实现View.OnClickListener接口
public class LoginActivity extends BaseActivity implements View.OnClickListener
//在onClick()函数中调用ImageView的点击响应函数;
case R.id.login_is_show_pwd: isShowPassword(); break;
1.2.2 setTransformationMethod方法解析
简介
setTransformationMethod是TextView的一个方法,EditText继承于TextView自然可以使用
这个方法是用来设置其中text的转换显示,接收的参数是TransformationMethod接口,系统给了我们几个默认实现:
(1)HideReturnsTransformationMethod 隐藏回车
(2)SingleLineTransformationMethod 不能用换行回车
(3)PasswordTransformationMethod 密码类型
(4)ReplacementTransformationMethod 抽象类,前面两个都是继承于这个抽象类,很明显就是替换,我们可以自己去继承这个类实现自己的TransformationMethod
小写转换为大写
(1)以密码编辑框对象调用setTransformationMethod方法;
pwdEt.setTransformationMethod(new AllCapTransformationMethod());
(2)自定义类继承ReplacementTransformationMethod方法;
public class AllCapTransformationMethod extends ReplacementTransformationMethod {
@Override
protected char[] getOriginal() {
char[] aa = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
return aa;
}
@Override
protected char[] getReplacement() {
char[] cc = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
return cc;
}
}
参考网址:EditText的setTransformationMethod的使用_小小程序员-CSDN博客_settransformationmethod