setTransformationMethod是TextView的一个方法,EditText继承于TextView自然可以使用
这个方法是用来设置其中text的转换显示
接收的参数是TransformationMethod接口,系统给了我们几个默认实现:
HideReturnsTransformationMethod隐藏回车
SingleLineTransformationMethod不能用换行回车
PasswordTransformationMethod密码类型
ReplacementTransformationMethod抽象类,前面两个都是继承于这个抽象类,很明显就是替换,我们可以自己去继承这个类实现自己的TransformationMethod
xml部分文件:
<RelativeLayout
android:id="@+id/olddiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/appname"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="10dp">
<EditText
android:id="@+id/editOldPass"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/sharp_text_layout"
android:hint="请输入原始密码"
android:inputType="textPassword"
android:paddingLeft="110dp"
android:shadowDx="@color/colorBlack"
android:textColor="@color/colorBlack"
android:textSize="16sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@drawable/sharp_text_layout"
android:text="原 密 码"
android:textColor="@color/colorBlack"
android:textSize="14sp" />
<CheckBox
android:id="@+id/password_check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:checked="false"
android:button="@mipmap/eye1"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/newdiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/appname"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="4dp">
<EditText
android:id="@+id/editNewPass"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_centerVertical="true"
android:background="@drawable/sharp_text_layout"
android:hint="请输入新密码"
android:inputType="textPassword"
android:paddingLeft="110dp"
android:textColor="@color/colorBlack"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@drawable/sharp_text_layout"
android:text="新 密 码"
android:textColor="@color/colorBlack"
android:textSize="16sp" />
<CheckBox
android:id="@+id/password_check2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:checked="false"
android:layout_marginRight="20dp"
android:button="@mipmap/eye1"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/newsdiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/appname"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="4dp">
<EditText
android:id="@+id/editNewPassAgain"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@drawable/sharp_text_layout"
android:hint="请确认新密码"
android:inputType="textPassword"
android:paddingLeft="110dp"
android:textColor="@color/colorBlack"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:background="@drawable/sharp_text_layout"
android:text="确认密码"
android:textColor="@color/colorBlack"
android:textSize="16sp" />
<CheckBox
android:id="@+id/password_check3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:checked="false"
android:button="@mipmap/eye1"/>
</RelativeLayout>
主要代码:
//明文显示
editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//密文显示
editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
//光标在最后
editText.setSelection(editText.length());
java部分文件:
EditText oldEditView = updatePassView.findViewById(R.id.editOldPass);
EditText newEditView = updatePassView.findViewById(R.id.editNewPass);
EditText confEditView = updatePassView.findViewById(R.id.editNewPassAgain);
CheckBox password_check1=updatePassView.findViewById(R.id.password_check1);
CheckBox password_check2=updatePassView.findViewById(R.id.password_check2);
CheckBox password_check3=updatePassView.findViewById(R.id.password_check3);
password_check1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
oldEditView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
oldEditView.setSelection(oldEditView.length());
password_check1.setButtonDrawable(R.mipmap.eye2);
}else {
oldEditView.setTransformationMethod(PasswordTransformationMethod.getInstance());
oldEditView.setSelection(oldEditView.length());
password_check1.setButtonDrawable(R.mipmap.eye1);
}
}
});
password_check2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
newEditView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
newEditView.setSelection(oldEditView.length());
password_check2.setButtonDrawable(R.mipmap.eye2);
}else {
newEditView.setTransformationMethod(PasswordTransformationMethod.getInstance());
newEditView.setSelection(newEditView.length());
password_check2.setButtonDrawable(R.mipmap.eye1);
}
}
});
password_check3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
confEditView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
confEditView.setSelection(confEditView.length());
password_check3.setButtonDrawable(R.mipmap.eye2);
}else {
confEditView.setTransformationMethod(PasswordTransformationMethod.getInstance());
confEditView.setSelection(confEditView.length());
password_check3.setButtonDrawable(R.mipmap.eye1);
}
}
});