2012-07-25 80 views
0
<RadioGroup> 

       <TableRow> 

        <RadioButton 
         android:id="@+id/a" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="lolo" 
         /> 

        <RadioButton 
         android:id="@+id/b" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="sh" 
         /> 
       </TableRow> 

       <TableRow> 

        <RadioButton 
         android:id="@+id/c" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="s" 
         /> 

        <RadioButton 
         android:id="@+id/d" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="cannot be determined" 
         /> 
       </TableRow> 
      </RadioGroup> 

我想選擇四個中的一個rdio按鈕,它像4個單獨的單選按鈕? ,得到選擇的所有R,我在Android的開發利用策略新非常和善引導我感謝名單wny單選按鈕組在代碼中不能正常工作

回答

1

RadioButton的意見必須是RadioGroup的直接子女,爲group-effect工作。

因此,如果您在RadioGroup下添加「TableRow」,則應該在RadioButton上看到的羣組性質將不起作用。

對於RadioButton的分組工作,嘗試這樣的事情。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" > 

    <RadioGroup 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <RadioButton 
      android:id="@+id/a" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="lolo" /> 

     <RadioButton 
      android:id="@+id/b" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="sh" /> 

     <RadioButton 
      android:id="@+id/c" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="s" /> 

     <RadioButton 
      android:id="@+id/d" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:text="cannot be determined" /> 
    </RadioGroup> 

</RelativeLayout> 

如果你這麼關心行列結構,然後用自己的佈局XML文件本身和執行單選按鈕setOnCheckedChangeListener(listener)。當任何單選按鈕被點擊時,相應的監聽器塊將被調用,然後取消選中其他單選按鈕。

1

我認爲你已經使用無線電組,定義該組這是正確的內部單選按鈕,而是指該鏈接Radio Button Group這可能是有用的

1

試試這個:

<RadioGroup 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

       <TableRow> 

        <RadioButton 
         android:id="@+id/a" 
         android:text="lolo" 
         android:button="@drawable/btn_touch"/> 

        <RadioButton 
         android:id="@+id/b" 
         android:text="sh" 
         android:button="@drawable/btn_touch"/> 
       </TableRow> 

       <TableRow> 

        <RadioButton 
         android:id="@+id/c" 
         android:text="s" 
         android:button="@drawable/btn_touch"/> 

        <RadioButton 
         android:id="@+id/d" 
         android:text="cannot be determined" 
         android:button="@drawable/btn_touch"/> 
       </TableRow> 
      </RadioGroup> 

其中btn_touch在繪製文件夾是:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <!-- When selected, use grey --> 
    <item android:drawable="@drawable/selected1" 
      android:state_checked="true"/> 
    <!-- When not selected, use white--> 
    <item android:drawable="@drawable/unselected1"/> 
</selector> 
+0

創建一個選中的複選框和另一個未選中的兩個圖像,它可以幫助你擺脫這個問題。 – AkashG 2012-07-25 10:10:02

+0

錯誤:錯誤:找不到與給定名稱匹配的資源(在'drawable'處,值爲'@ drawable/selected1')。 – dashh 2012-07-25 10:42:50

+0

@ dreams13你必須在你的可繪製文件夾中放入兩個名爲selected1和unselected1的圖像。 – AkashG 2012-07-25 10:46:11

0

也許這可以幫助你,如果你想保持分離行的按鈕: 1.從XML刪除組,因爲你不需要它:在

<TableRow> 
        <RadioButton 
         android:id="@+id/a" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="lolo" /> 

        <RadioButton 
         android:id="@+id/b" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="sh" /> 
       </TableRow> 

       <TableRow> 
        <RadioButton 
         android:id="@+id/c" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="s" /> 

        <RadioButton 
         android:id="@+id/d" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="cannot be determined" /> 
       </TableRow> 

2.使用這個你活動,使其工作:

private RadioButton a; 
    private RadioButton b; 
    private RadioButton c; 
    private RadioButton d; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ... 
     ButtonChange clRadio = new ButtonChange(); 
     a = (RadioButton) findViewById(R.id.a); 
     b = (RadioButton) findViewById(R.id.b); 
     c = (RadioButton) findViewById(R.id.c); 
     d = (RadioButton) findViewById(R.id.d); 
     a.setOnCheckedChangeListener(clRadio); 
     b.setOnCheckedChangeListener(clRadio); 
     c.setOnCheckedChangeListener(clRadio); 
     d.setOnCheckedChangeListener(clRadio); 
     ... 
    } 
    public class ButtonChange implements OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton radio, boolean isChecked) { 
      if(isChecked) 
       switch(radio.getId()) { 
       case R.id.a: 
       case R.id.b: 
       case R.id.c: 
       case R.id.d: 
        a.setChecked(false); 
        b.setChecked(false); 
        c.setChecked(false); 
        d.setChecked(false); 
        radio.setChecked(true); 
        break; 
       default : 
        break; 
       } 
      } 
     } 
    } 

這應該做的伎倆。