2011-05-24 60 views
8

是否有可能在他們自己的佈局有單選按鈕的無線電組?每個單選按鈕必須位於包含文本和圖像的行上。我不會使用列表視圖,因爲我只有2行。RadioGroup與單選按鈕在不同的佈局

非常感謝,

的Gratzi

回答

7

下面是做到這一點的方式:

上的XML佈局文件每個RadioButton創建一個單獨的RadioGroup。例如:現在

<RadioGroup 
    android:id="@+id/rdoFooWrapper" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <RadioButton 
     android:id="@+id/rdoFoo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</RadioGroup> 

<RadioGroup 
    android:id="@+id/rdoBooWrapper" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <RadioButton 
     android:id="@+id/rdoBoo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</RadioGroup> 

,Java源代碼中,做這樣的事情:

rdoFooWrapper = (RadioGroup) findViewById(R.id.rdoFooWrapper); 
rdoFoo = (RadioButton) findViewById(R.id.rdoFoo); 
rdoBooWrapper = (RadioGroup) findViewById(R.id.rdoBooWrapper); 
rdoBoo = (RadioButton) findViewById(R.id.rdoBoo); 

rdoFoo.setOnCheckedChangeListener(this); // implement OnCheckedChangeListener to the current class 
rdoBoo.setOnCheckedChangeListener(this); 

// ... 

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
{ 
    rdoFooWrapper.clearCheck(); 
    rdoBooWrapper.clearCheck(); 
}