实现显示和隐藏密码
<LinearLayout
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dp"
android:background="@drawable/border2"
>
<EditText
android:id="@+id/Account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="账号"
android:singleLine="true"
android:background="#fff"
android:padding="12dp"
android:inputType="text"
android:textColorHint="#0092FF"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="12dp"
android:hint="密码"
android:inputType="textPassword"
android:singleLine="true"
android:textColorHint="#FF0033"/>
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:button="@drawable/fuhe"/>
</RelativeLayout>
</LinearLayout>
背景布局
LinearLayout的边框背景设置:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:width="1dp" android:color="@color/black"/>
<solid android:color="@color/white"/>
</shape>
密码的显示和隐藏设置:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_baseline_visibility_off_24" android:state_checked="false"/>
<item android:drawable="@drawable/ic_baseline_visibility_24" android:state_checked="true"/>
</selector>
java代码设置
通过点击眼睛显示和隐藏EditText的密码
private void init(){
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else {
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
效果展示
- edit.requestFocus(); //请求获取焦点
- edit.clearFocus(); //清除焦点