0

我在 xml 中有 RadioGroup。我正在用代码中的 RadioButton 填充它。问题是,在模拟器中,RadioButtons 的行为与 RadioGroup 不同。我可以一次选择所有单选按钮。但一切似乎都在真实设备上运行良好。

以下是所有文件的屏幕截图和代码:

模拟器截图

模拟器截图

设备三星 Galaxy 的屏幕截图

设备三星 Galaxy 的屏幕截图

MainActivity.java 的源代码

public class MainActivity extends Activity {
/** Called when the activity is first created. */
private RadioGroup rg;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LayoutInflater inflater = LayoutInflater.from(getBaseContext());
    rg = (RadioGroup)findViewById(R.id.rg);

    for (int i = 0; i < 5; i++) {
        inflater.inflate(R.layout.myradio, rg,true);
    }

}

}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/llContainer"
    tools:context=".MainActivity" >


<RadioGroup 
    android:id="@+id/rg"
    android:background="#0000ff"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

</RadioGroup>

myradio.xml

<?xml version="1.0" encoding="utf-8"?>

<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="RadioButton" />
4

1 回答 1

0

会是这样

public class example extends Activity {
    /** Called when the activity is first created. */
    private RadioGroup rgroup;
     RadioButton rb;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.example);


        rgroup = (RadioGroup)findViewById(R.id.rg);
        final RadioButton[] rb = new RadioButton[5];
        for (int i = 0; i < 5; i++) {
            rb[i]=new RadioButton(this);
            rb[i].setText("rdo"+i);
            rb[i].setId(i);
            rgroup.addView(rb[i]);


        }

    }

    }

在此处输入图像描述

于 2013-01-21T10:31:05.047 回答