Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 动态添加单选按钮时在radiogroup中出现问题_Android_Android Radiogroup - Fatal编程技术网

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

Android 动态添加单选按钮时在radiogroup中出现问题,android,android-radiogroup,Android,Android Radiogroup,我没有看到任何类似于我的问题的帖子。我有一个radiogroup,它有两个按钮,如下所示: <RadioGroup android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:id

我没有看到任何类似于我的问题的帖子。我有一个
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>
一旦选中该radiogroup的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
通过设置
按钮。setChecked(false)何时
其他按钮也被检查过。但它不起作用。我不知道这是什么原因
这是个问题


谁能帮帮我吗?谢谢

不要通过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>
然后,您就可以使用setOnCheckedChangeListener了:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) 
            {
                    //your code
            });

在初始化RadioButton时使用活动类上下文

不要使用
活动。此
使用
main活动。此

以下是完整的工作代码..在您的活动中:

        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>


您是否为所有视图(RadioGroup&RadioButton)设置了id?是的,我设置了。对于这个新按钮,我使用了
在checkedChangeListener
上查看您的
。code我已经编辑了我的问题。请看一看,汉克斯,实际上我用的是AppCompactRadioButton,这就是问题所在。哦,活动是我的课。谢谢,你可以使用AppCompatRadioButton…还有
        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();
<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>