Android:在一个活动中重构多组单选按钮

时间:2017-02-07 09:44:57

标签: android radio-button kotlin

我想在一个活动中创建许多广播组,例如

enter image description here

关注Android文档https://developer.android.com/guide/topics/ui/controls/radiobutton.html,使数据绑定到Activity以获取类似" object1 = true,object2 = false"的值。我创建了2个函数来从2个无线电组中获取数据。

在xml文件中,代码为

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Object 1."
    android:textColor="#000000"
    android:textSize="24dp" />

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/pass_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked1"
        android:text="pass" />

    <RadioButton
        android:id="@+id/not_pass_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked1"
        android:text="not pass" />

    <RadioButton
        android:id="@+id/not_available_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked1"
        android:text="not available" />

</RadioGroup>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Object 2."
    android:textColor="#000000"
    android:textSize="24dp" />

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/pass_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked1"
        android:text="pass" />

    <RadioButton
        android:id="@+id/not_pass_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked1"
        android:text="not pass" />

    <RadioButton
        android:id="@+id/not_available_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onRadioButtonClicked1"
        android:text="not available" />

</RadioGroup>

,我创建了两个这样的函数:

fun onRadioButtonClicked1(view: View): Boolean? {
    val checked = (view as RadioButton).isChecked
    var value: Boolean? = null

    when (view.id) {
        R.id.pass_1 -> if (checked) value = true
        R.id.not_pass_1 -> if (checked) value = false
        R.id.not_available_1 -> if (checked) value = null
    }
    return value
}

fun onRadioButtonClicked2(view: View): Boolean? {
    val checked = (view as RadioButton).isChecked
    var value: Boolean? = null

    when (view.id) {
        R.id.pass_2 -> if (checked) value = true
        R.id.not_pass_2 -> if (checked) value = false
        R.id.not_available_2 -> if (checked) value = null
    }
    return value
}

它是否有一些逻辑来重构这些东西(比如重用这个函数)?

2 个答案:

答案 0 :(得分:0)

在所有onClick s

中调用相同的函数

android:onClick="onRadioButtonClicked"

然后将所有6个case添加到when语句

fun onRadioButtonClicked(view: View): Boolean? {
val checked = (view as RadioButton).isChecked
var value: Boolean? = null

when (view.id) {
    R.id.pass_1 -> if (checked) value = true
    R.id.not_pass_1 -> if (checked) value = false
    R.id.not_available_1 -> if (checked) value = null
    R.id.pass_2 -> if (checked) value = true
    R.id.not_pass_2 -> if (checked) value = false
    R.id.not_available_2 -> if (checked) value = null
}
return value

}

答案 1 :(得分:0)

第一个明显的方法:

var value1: Boolean? = null
var value2: Boolean? = null

fun onRadioButtonClicked(view: View) {
    val checked = (view as RadioButton).isChecked

    when (view.id) {
        R.id.pass_1 -> if (checked) value1 = true
        R.id.not_pass_1 -> if (checked) value1 = false
        R.id.not_available_1 -> if (checked) value1 = null
        R.id.pass_2 -> if (checked) value2 = true
        R.id.not_pass_2 -> if (checked) value2 = false
        R.id.not_available_2 -> if (checked) value2 = null
    }
}

但是如果你有足够的RadioGroup,另一种方法是为每个RadioButton设置Tag on XML(我用于下面的字符串“TRUE”,“FALSE”):

var value1: Boolean? = null
var value2: Boolean? = null

fun onRadioButtonClicked1(view: View) = onRadioButtonClicked(view) { value1 = it }
fun onRadioButtonClicked2(view: View) = onRadioButtonClicked(view) { value2 = it }

fun onRadioButtonClicked(view: View, function: (Boolean?) -> Unit) {
    val checked = (view as RadioButton).isChecked

    if (checked) {
        view.tag
                .let {
                    when (it) {
                        "TRUE" -> true
                        "FALSE" -> false
                        else -> null
                    }
                }
                .let { function.invoke(it) }
    }
}

我没有检查,但我认为你有更合适的回调,比如RadioButton或RadioGroup上的onCheckedChanged。