【问题标题】:How to change the background color of a RadioGroup at runtime?如何在运行时更改 RadioGroup 的背景颜色?
【发布时间】:2014-04-23 04:18:41
【问题描述】:

我正在尝试在单选组中动态生成单选按钮。我有一个具有三个radioButtons 的radioGroup。在运行时,我想验证如果没有选中单选按钮,请将 radioGroup 的背景颜色设置为红色,表示警告。

For the purpose I am using

public String validate() {
    if (radioGroup.findViewById(radioGroup.getCheckedRadioButtonId()) != null){
        return "SUCCESS";
    } else {
        radioGroup.setBackgroundResource(R.drawable.red_border);
        return "failure";
    }

}

但在这里,radioGroup 的背景并没有改变,我已经尝试了类似的方法来处理 editText 和 DatePicker 的所有工作。

【问题讨论】:

  • 这段代码对我有用...至少设置背景。

标签: android android-layout radio-button radio-group


【解决方案1】:

我们可以通过radioGroup子项循环来做到这一点

for (int i = 0; i < radioGroup.getChildCount(); ++i) {
    ((RadioButton) radioGroup.getChildAt(i))
    .setBackgroundResource(R.drawable.red_border);
}

这将遍历 RadioGroup 的每个子节点,这样我们就可以实现所需的功能

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    相关资源
    最近更新 更多