【问题标题】:Adding radiobutton within radiobutton like parent-child sub menu andfood在单选按钮中添加单选按钮,例如父子菜单和食物
【发布时间】:2021-12-25 07:21:24
【问题描述】:

多个和父子子单选按钮。 例如

○Radiobutton1  

         ○Subradiobutton1-1

         ○Subradiobutton1-2  

○Radiobutton2    
         ○Subradiobutton2-1  
        ○Subradiobutton2-2    

如何在java活动android中实现

我想要的是基于单选按钮执行操作: 例如。 如果我选择 Radiobutton1 它将执行 Subradiobutton1-1 和 Subradiobutton1-2 下指定的所有操作;要么;如果我选择 Subradiobutton1-2,它将只执行该按钮下指定的操作。

我知道如何添加 radio.group 但我想在单选按钮中添加子单选按钮

【问题讨论】:

  • 这个问题再解释一下!

标签: java android radio-button


【解决方案1】:

单选按钮用于从一组选项中选择一个选项。因此,您可以放置​​子复选框而不是子单选按钮,并为所有单选按钮设置这些复选框的 id 数组。然后,您可以为单选按钮定义一个 onClick 方法,该方法将选中所选单选按钮下的所有复选框。

int checkBoxIds[][];
//callback for when a radio button is selected/disselcted
public void onSelectionChange(int radioButtonNo, boolean allSelected){
    for(int checkBoxId: checkBoxIds[radioButtonNo]){
        if(allSelected){
            findViewById<CheckBox>(checkBoxIds[radioButtonNo]).setChecked(true);
        }
        else{
            findViewById<CheckBox>(checkBoxIds[radioButtonNo]).setChecked(false);
        }
    }
}
//onClick method for radio buttons
public void onRadioButtonClicked(View view) {
    boolean checked = ((RadioButton) view).isChecked();
    switch(view.getId()) {
        case R.id.radio_button0:
            onSelectionChange(0, checked);
            break;
        case R.id.radio_button1:
            onSelectionChange(1, checked);
            break;
        case R.id.radio_button2:
            onSelectionChange(2, checked);
            break;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-05
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 2013-02-05
    • 1970-01-01
    • 2013-03-20
    相关资源
    最近更新 更多