代码之家  ›  专栏  ›  技术社区  ›  Jyoti JK

在radiogroup中动态添加单选按钮时出现问题

  •  1
  • Jyoti JK  · 技术社区  · 8 年前

    我没有看到任何类似于我的问题的帖子。我有一个 radiogroup 它有两个按钮,如下所示,

    <RadioGroup
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <RadioButton
                android:id="@+id/basic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
            <RadioButton
                android:id="@+id/advanced"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </RadioGroup>
    

    在我的申请中,有时我需要 我又动态添加了一个按钮 喜欢

    button=new AppCompatRadioButton(Activity.this);                                                              
    button.setId(R.id.my_custom_id);                                                                                  
    radiogroup.addView(button);
    

    一旦选中该组的ChangeListener代码,

    (radioGroup).setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        if(checkedId== R.id.basic) {
                            button.setChecked(false);
                            //my code
                        }
                        else if(checkedId== R.id.advanced) {
                            button.setChecked(false);
                            //my code
                        }
                        else {
                           //it is newly added button's part
                        }
                    }
                });
    

    问题是,当选中新添加的按钮时,它不是 单击其他按钮时未选中。我试着解决这个问题 OnCheckedChangeListener 通过设置 button.setChecked(false); 什么时候 检查了其他按钮。但它不起作用。我不知道是哪个

    谁能帮帮我吗。谢谢

    2 回复  |  直到 8 年前
        1
  •  2
  •   Mo Faizan Shaikh    8 年前

    不要通过java代码以编程方式制作单选按钮,而是这样做,即将该单选按钮放在xml文件中,但首先将android:visibility设置为gone,这将确保最后一个单选按钮不可见:

    <RadioGroup
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RadioButton
            android:id="@+id/basic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:id="@+id/advanced"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    
        <RadioButton
            android:id="@+id/new_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility:"gone"
            />
    </RadioGroup>
    

    当需要使用单选按钮时,在java类中,使单选按钮再次可见,如下所示:

    button.setVisibility(View.VISIBLE);
    

    然后,您可以使用setOnCheckedChangeListener:

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) 
                {
                        //your code
                });
    
        2
  •  1
  •   rafsanahmad007    8 年前

    初始化单选按钮时使用活动类上下文。

    而不是 Activity.this 使用 MainActivity.this

    这是完整的工作代码。。在您的活动中:

            RadioGroup group = (RadioGroup) findViewById(R.id.group);
    
            group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    Log.d("chk", "id" + checkedId);
    
                    if (checkedId == R.id.basic) {
                        //some code
                        Toast.makeText(MainActivity.this, "First", Toast.LENGTH_SHORT).show();
                    } else if (checkedId == R.id.advanced) {
                        //some code
                        Toast.makeText(MainActivity.this, "Second", Toast.LENGTH_SHORT).show();
                    } else if (checkedId == R.id.my_custom_id) {
                        //some code
                        Toast.makeText(MainActivity.this, "Third", Toast.LENGTH_SHORT).show();
                    }
    
                }
    
            });
            RadioButton button = new RadioButton(this);
            button.setId(R.id.my_custom_id);
            button.setText("Dynamic Button");
            group.addView(button);
            group.clearCheck();
    

    在布局Xml中:

    <RadioGroup
        android:id = "@+id/group"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <RadioButton
            android:id="@+id/basic"
            android:layout_width="wrap_content"
            android:text="Basic"
            android:layout_height="wrap_content"
            />
        <RadioButton
            android:id="@+id/advanced"
            android:layout_width="wrap_content"
            android:text="Advanced"
            android:layout_height="wrap_content"
            />
    </RadioGroup>