1.在drawable里面创建xml文件
2.示例(点赞效果)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/agree_red" android:state_checked="true" />
<item android:drawable="@drawable/agree_black" />
</selector>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check_password"
android:layout_gravity="center"
android:layout_marginTop="30dp"/>
2.示例(显示密码隐藏密码效果)
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:password="true"/>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check_password"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
fincheck();
check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (check.isChecked()){
//TODO 显示密码
password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
}else {
//TODO 隐藏密码
password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
}
});
}
private void fincheck(){
check=findViewById(R.id.check);
password=findViewById(R.id.password);
}