android涓鸿嚜瀹氫箟CompoundButton娣诲姞娑熸吉鏁堟灉
瑕佷负鑷畾涔夌殑CompoundButton娣诲姞娑熸吉鏁堟灉锛屽彲浠ユ寜鐓т互涓嬫楠よ繘琛岋細
- 鍒涘缓涓€涓柊鐨刣rawable鏂囦欢ripple_effect.xml锛岀敤浜庡畾涔夋稛婕晥鏋滅殑鏍峰紡鍜岄鑹诧細
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/ripple_effect_color">
<item android:id="@android:id/mask">
<shape android:shape="oval">
<solid android:color="@android:color/white" />
</shape>
</item>
</ripple>
涓婅堪浠g爜涓紝ripple_effect_color
涓烘稛婕晥鏋滅殑棰滆壊锛屽彲浠ユ牴鎹嚜宸辩殑闇€姹傝繘琛岃皟鏁淬€?/p>
- 鍦ㄨ嚜瀹氫箟CompoundButton鐨勫竷灞€鏂囦欢涓紝灏嗘稛婕晥鏋滅殑drawable鏂囦欢浣滀负鑳屾櫙璁剧疆缁機ompoundButton锛?/li>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/custom_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ripple_effect"
... />
<!-- 鍏朵粬甯冨眬浠g爜 -->
</RelativeLayout>
鍦ㄤ笂杩颁唬鐮佷腑锛屽皢娑熸吉鏁堟灉鐨刣rawable鏂囦欢ripple_effect
璁剧疆涓篊heckBox鐨勮儗鏅€?/p>
- 鍦ㄨ嚜瀹氫箟CompoundButton鐨凧ava浠g爜涓紝涓篊ompoundButton璁剧疆鐐瑰嚮浜嬩欢鐩戝惉鍣紝骞跺湪鐐瑰嚮浜嬩欢涓鐞嗘稛婕晥鏋滐細
CheckBox customCheckbox = findViewById(R.id.custom_checkbox);
customCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// 澶勭悊閫変腑鐘舵€佷笅鐨勬稛婕晥鏋?/span>
buttonView.setBackgroundResource(R.drawable.ripple_effect);
} else {
// 澶勭悊鏈€変腑鐘舵€佷笅鐨勬稛婕晥鏋?/span>
buttonView.setBackgroundResource(0);
}
}
});
鍦ㄤ笂杩颁唬鐮佷腑锛屽綋CompoundButton鐨勯€変腑鐘舵€佹敼鍙樻椂锛屾牴鎹甶sChecked鐨勫€兼潵澶勭悊娑熸吉鏁堟灉銆傚鏋滆閫変腑锛岃缃负娑熸吉鏁堟灉鐨刣rawable鏂囦欢锛涘鏋滄湭琚€変腑锛岃缃负null鎴栬€?銆?/p>
杩欐牱锛屽氨鍙互涓鸿嚜瀹氫箟鐨凜ompoundButton娣诲姞娑熸吉鏁堟灉浜嗐€?/p>
相关问答