我的布局xml看起来像
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollview1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearlayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
我正在创建radiogroup并通过代码添加radiobuttons
LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);
RadioGroup rg = new RadioGroup(this);
rg.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
rg.setOrientation(0);
while(itr.hasNext()){
final String drinkname = itr.next();
System.out.println("drinkname: "+drinkname);
RadioButton rb = new RadioButton(this);
rb.setText(drinkname);
rg.addView(rb);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//group.clearCheck();
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
System.out.println(selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
});
ll.addView(rg);
应该显示6个值;但它只显示3个正常,剩下的看起来像隐藏。有人可以帮我吗?
提前致谢!!
答案 0 :(得分:0)
使用当前的RadioGroup
类实现,您不能有两行RadioButtons
。
因此,您可以为布局设置HorizontalScrollView
,并允许用户滚动查看所有RadioButtons
,或者使用两个RadioGroups
,每个RadioButtons
包含3 LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout1);
RadioGroup rg = new RadioGroup(this);
RadioGroup rg2 = new RadioGroup(this);
rg.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rg.setOrientation(LinearLayout.HORIZONTAL);
rg2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rg2.setOrientation(LinearLayout.HORIZONTAL);
String [] drinks = new String [] {"Mountain Dew", "7Up", "Root Beer", "Pepsi", "Cola", "Ice Tea"};
int i = 0;
while(i<drinks.length){
final String drinkname = drinks[i];
Log.i(TAG, "drinkname: "+drinkname);
RadioButton rb = new RadioButton(this);
rb.setText(drinkname);
if(i %2==0)
rg.addView(rb);
else
rg2.addView(rb);
i++;
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//unselect all radios in the other RadioGroup
if(rg2.getCheckedRadioButtonId()!=-1){
rg2.setOnCheckedChangeListener(null);
rg2.clearCheck();
rg2.setOnCheckedChangeListener(this);
}
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
if(selectrb != null){
Log.i(TAG,selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//unselect all radios in the other RadioGroup
if(rg.getCheckedRadioButtonId()!=-1){
rg.setOnCheckedChangeListener(null);
rg.clearCheck();
rg.setOnCheckedChangeListener(this);
}
int id = group.getCheckedRadioButtonId();
RadioButton selectrb = (RadioButton) findViewById(id);
if(selectrb != null){
Log.i(TAG,selectrb.getText().toString());
WebMenu.drinkname = selectrb.getText().toString();
}
}
});
ll.addView(rg);
ll.addView(rg2);
。
因此,使用您的示例(几乎没有变化),最后的解决方案可以像这样实现:
RadioGroups
你必须要小心,你的两个RadioButtons
每个只跟踪3 RadioButton
。它们不相互通信,不会相互影响。因此,如果您从第一行(第一个RadioGroup
)中选择一个RadioGroup
,您仍然可以选择第二个radioButtons
,反之亦然。你必须自己管理这种情况。
编辑:管理此操作的一种方法是取消选择RadioGroup1中的所有RadioButton
,当我们在RadioGroup2中选择一个{{1}}时,反之亦然。我添加了代码来做到这一点。