체크박스와 라디오버튼은 그냥 기본을 써도 괜찮은데, 디자이너들은 기본 컴포넌트를 쓰는 걸 아주 싫어한다…
이번엔 선택된 체크박스의 글자를 bold로 바꿔달라고 한다. setOnCheckedChangeListener
에 달면 2way binding일 때 문제가 생기기 때문에 setChecked
에 넣어주는게 좋다.
class BoldCheckBox : AppCompatCheckBox { constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet) : super(context, attrs) constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) override fun setChecked(checked: Boolean) { super.setChecked(checked) setTypeface(null, if (checked) Typeface.BOLD else Typeface.NORMAL) } }
라디오버튼도 마찬가지로 이렇게 하면 된다.
class BoldRadioButton : AppCompatRadioButton { constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet) : super(context, attrs) constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) override fun setChecked(checked: Boolean) { super.setChecked(checked) setTypeface(null, if (checked) Typeface.BOLD else Typeface.NORMAL) } }
AppCompatCheckBox
를 상속받아야 style.xml
에서 checkboxStyle
로 지정한 스타일이 적용된다.
<item name="checkboxStyle">@style/DefaultCheckBox</item>