【问题标题】:clear all child of a view (RadioGroup) [duplicate]清除视图的所有子项(RadioGroup)[重复]
【发布时间】:2015-12-23 06:44:19
【问题描述】:

我有一个抽屉菜单,里面有一个动态选项,我必须填满它。 我想清除adapter 内的RadioGroup 的所有孩子,但我什么也没找到。

p.s:我用addView填充所有RadioGroup并用CheckBox填充它

ps2:问​​题不在于勾选和取消勾选。我想了解如何删除视图的所有子视图(如 RadioGroup)。

viewHolder.mRadioGroup.setVisibility(View.VISIBLE);
/////clear viewHolder.mRadioGroup child hear, befor add new child to it
for (int i; i<10; i++) {
   RadioButton radioButton = new RadioButton(mContext);
   radioButton.setText(selectableValue.getValue());
   viewHolder.mRadioGroup.addView(radioButton);
}

【问题讨论】:

  • radioGroup.clearCheck();
  • @DhawalSodhaParmar 我想清除孩子,不清除检查

标签: android checkbox view


【解决方案1】:

试试这个:

    int count = radioGroup.getChildCount();
    if(count>0) {
       for (int i=count-1;i>=0;i--) {
           View o = radioGroup.getChildAt(i);
           if (o instanceof RadioButton) {
               radioGroup.removeViewAt(i);
           } 
       } 
    }

【讨论】:

  • @Ashkan 如果要删除所有视图,请从代码中删除 if (o instanceof RadioButton)
  • 不,项目不止于此。而我当时无权执行此操作。
  • 我听不懂你。你想做什么。请重新编辑问题。更清楚
  • 请解释一下**if(o instanceof RadioButton) ** plz
  • 它会检查子视图 RadioButton 是否只有它会被移除。如果你移除它会移除里面的所有子元素。
【解决方案2】:

这取自链接here,用于复杂的嵌套布局

private static void disable(ViewGroup layout) {
    layout.setEnabled(false);
    for (int i = 0; i < layout.getChildCount(); i++) {
        View child = layout.getChildAt(i);
        if (child instanceof ViewGroup) {
            disable((ViewGroup) child);
        } else { 
            child.setEnabled(false);
        } 
    } 
} 

【讨论】:

    猜你喜欢
    • 2015-09-09
    • 2011-01-08
    • 2017-07-26
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多