【问题标题】:Issue in radiogroup while adding radiobuttons dynamically动态添加单选按钮时单选组中的问题
【发布时间】:2017-11-24 11:04:01
【问题描述】:

我没有看到任何与我的问题类似的帖子。我有一个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);

该无线电组的 Oncheckedchangelistener 代码,

(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); 时 检查了其他按钮。但它不起作用。我不知道哪个使 问题。

谁能帮帮我。谢谢!

【问题讨论】:

  • 你为所有视图设置了id吗(RadioGroup&RadioButton)?
  • 是的,我做到了。对于那个新按钮,我使用了&lt;resources&gt; &lt;item name="my_custom_id" type="id" /&gt; &lt;/resources&gt; i
  • 分享你的OnCheckedChangeListener..code
  • 我已经编辑了我的问题。请看一下

标签: android android-radiogroup


【解决方案1】:

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

<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】:

    在初始化 RadioButton 时使用您的 Activity 类上下文。

    MainActivity.this代替Activity.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>
    

    【讨论】:

    • 谢谢,实际上我使用了 AppCompactRadioButton,这就是问题所在。
    • 哎呀,活动是我的课。非常感谢
    • 您可以使用 AppCompatRadioButton ...也
    猜你喜欢
    • 2012-03-11
    • 2011-01-04
    • 1970-01-01
    • 2018-07-04
    • 2011-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    相关资源
    最近更新 更多