I am new to kotlin and I am having some difficulty with checkboxes and radio buttons. I am trying to use radio buttons with checkboxes to store info. When the user clicks on the day(s) radio button, they should be able to use the checkboxes to select the days that are at the checkboxes. When I ran the debugger, I learned that the function skips over the checkboxes. I am guessing the problem is with the setOnCheckedLister. What might be the best way to use the checkboxes with the radio button? This is what my function looks like.
private fun saveSchedule() {
var schedule = Schedule().apply {
radio_group.setOnCheckedChangeListener { group, checkedId ->
if (checkedId == R.id.radio_days) {
when {
monday.isChecked -> {
day = monday.text.toString()
}
tuesday.isChecked -> {
day = tuesday.text.toString()
}
wednesday.isChecked -> {
day = wednesday.text.toString()
}
thursday.isChecked -> {
day = thursday.text.toString()
}
friday.isChecked -> {
day = friday.text.toString()
}
saturday.isChecked -> {
day = saturday.text.toString()
}
sunday.isChecked -> {
day = sunday.text.toString()
}
}
}
This is what my XML looks like.
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radio_days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radiobtndays"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- checkboxes for days -->
<CheckBox android:id="@+id/monday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkboxmonday"
android:enabled="true"
/>
<CheckBox android:id="@+id/tuesday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkboxtuesday"
android:enabled="true"
/>
<CheckBox android:id="@+id/wednesday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkboxwednesday"
android:enabled="true"
/>
<CheckBox android:id="@+id/thursday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkboxthursday"
android:enabled="true"
/>
<CheckBox android:id="@+id/friday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkboxfriday"
android:enabled="true"
/>
<CheckBox android:id="@+id/saturday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkboxsaturday"
android:enabled="true"
/>
<CheckBox android:id="@+id/sunday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checkboxsunday"
android:enabled="true"
/>
</LinearLayout>
Aucun commentaire:
Enregistrer un commentaire