单选按钮没有正确地在动态创建RadioGroup中选择/取消 [英] RadioButtons Don't Properly Select/Deselect in Dynamically Created RadioGroup

查看:623
本文介绍了单选按钮没有正确地在动态创建RadioGroup中选择/取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建一个XML布局文件RadioGroup中一切都很好,但是当我动态创建它的另一个选择时,单选按钮不会取消选择:

这里的code:

 公共类MainActivity延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    RadioGroup中radioGroup中=(RadioGroup中)findViewById(R.id.radioGroup1);    单选radioButtonView =新的单选按钮(本);
    radioButtonView.setText(单选);
    radioGroup.addView(radioButtonView);    单选radioButtonView2 =新的单选按钮(本);
    radioButtonView2.setText(RadioButton2);
    radioGroup.addView(radioButtonView2);
}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.activity_main,菜单);
    返回true;
}
}

和布局文件:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=MainActivity。>< RadioGroup中
    机器人:ID =@ + ID / radioGroup1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真正的>
< / RadioGroup中>
< / RelativeLayout的>


解决方案

您需要设置一些标识为您的单选按钮,因为这样的:

  INT idRadio =<有的数字取代;
radioButtonView.setId(idRadio ++);
radioButtonView2.setId(idRadio ++);

一旦他们有独特的ID,它应该工作。只需确保ID不与任何现有的图形元素碰撞,而不是零(去你的根文件夹中,并期待在R.java为其他元素ID)。

When I create a RadioGroup in an XML layout file everything's fine, but when I create it dynamically the RadioButtons don't deselect when another is selected:

Here's the code:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);

    RadioButton radioButtonView = new RadioButton(this);
    radioButtonView.setText("RadioButton");
    radioGroup.addView(radioButtonView);

    RadioButton radioButtonView2 = new RadioButton(this);
    radioButtonView2.setText("RadioButton2");
    radioGroup.addView(radioButtonView2);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

And the layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true" >
</RadioGroup>
</RelativeLayout>

解决方案

You need to set some sort of ID for your radio button, as such:

int idRadio = <some number>;
radioButtonView.setId(idRadio++);
radioButtonView2.setId(idRadio++);

Once they have distinct IDs, it should work. Just make sure the IDs don't collide with any existing graphical element, and is not zero (go to your "gen" folder and look at R.java for the other element IDs).

这篇关于单选按钮没有正确地在动态创建RadioGroup中选择/取消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆