2010-03-02 34 views
11

我有多個單選按鈕,我想使用表格進行佈局,但也將它們包括在單個無線組中。我有以下XML佈局:如何將無線電組添加到表格內的單選按鈕?

<RadioGroup android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/Group1"> 

    <TableLayout android:id="@+id/RadioButtons" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 

     <TableRow> 
      <RadioButton android:id="@+id/rad1" 
       android:text="RButton1" 
       android:layout_width="105px" 
       android:layout_height="wrap_content" 
       android:textSize="13px"></RadioButton> 
      <RadioButton android:id="@+id/rad2" 
       android:text="RButton2" 
       android:layout_width="105px" 
       android:textSize="13px" 
       android:layout_height="wrap_content"></RadioButton> 
      <RadioButton android:id="@+id/rad3" 
       android:text="RButton3" 
       android:layout_width="105px" 
       android:textSize="13px" 
       android:layout_height="wrap_content"></RadioButton> 
     </TableRow> 
     </TableLayout> 
</RadioGroup> 

但不幸的是表內的單選按鈕似乎忽略一個事實,即他們是RadioGroup中的標籤內,因爲這樣你可以選擇在一個以上的單選按鈕時間。我注意到,通過刪除表,只是有單選按鈕,它工作得很好。我該如何克服這一點?它會像在表格內而不是在外面聲明無線電組一樣簡單嗎?謝謝你的幫助。

回答

7

你的RadioButton小部件必須是RadioGroup的直接子項才能工作。

+0

所以沒有用桌子構造按鈕的方法,讓它們成爲無線電組的一部分? – Fizz 2010-03-02 13:24:37

+0

只有'RadioButton'小部件是'RadioGroup'的直接子節點。因此,您的'RadioButton'小部件需要位於表格單個單元格的行或列中。 – CommonsWare 2010-03-02 13:43:29

+0

如果我編程地將每個單選按鈕分配到一個組中,該怎麼辦? – nobalG 2014-11-07 14:15:08

0
rg1 = (RadioGroup)findViewById(R.id.radioGroup1); 
     rg2 = (RadioGroup)findViewById(R.id.radioGroup2); 
     rg1.setOnCheckedChangeListener(this); 
     rg2.setOnCheckedChangeListener(this); 
    } 
    boolean rg1b = false; 
    boolean rg2b = false; 

    @Override 
    public void onCheckedChanged(RadioGroup rgId, int radioButtonId) { 
     switch (rgId.getId()) { 
     case R.id.radioGroup1: 
      rg1b=true; 
      if(rg2b) 
       rg2.clearCheck(); 
      break; 

     case R.id.radioGroup2: 
      rg1b=true; 
      if(rg1b) 
       rg1.clearCheck(); 
      break; 
     } 
+4

請不要只是轉儲代碼作爲您的答案。解釋會很有用。 – 2013-08-03 03:05:36

2

這是我的RadioGroup/RadioButton擴展(SoftRadioGroup/SoftRadioButton)。 RadioGroup在佈局XML中不再需要。您可以將RadioButton分組爲一個名爲group的屬性。

SoftRadioButton:

import java.util.HashMap; 
import java.util.Random; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.widget.RadioButton; 

public class SoftRadioButton extends RadioButton { 

    private static HashMap<String, SoftRadioGroup> GROUP_MAPPINGS = new HashMap<String, SoftRadioGroup>(); 
    private String mGroupName; 

    public SoftRadioButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     addToGroup(attrs); 
    } 

    public SoftRadioGroup getRadioGroup() { 
     return GROUP_MAPPINGS.get(mGroupName); 
    } 

    private void addToGroup(AttributeSet attrs) { 
     for (int i = 0; i < attrs.getAttributeCount(); i++) { 
      if (attrs.getAttributeName(i).equals("group")) { 
       String groupName = attrs.getAttributeValue(i); 
       SoftRadioGroup group; 
       if ((group = GROUP_MAPPINGS.get(groupName)) != null) { 
        // RadioGroup already exists 
        group.addView(this); 
        setOnClickListener(group); 
        mGroupName = groupName; 

       } else { 
        // this is the first RadioButton in the RadioGroup 
        group = new SoftRadioGroup(); 
        group.addView(this); 
        mGroupName = groupName; 
        setOnClickListener(group); 

        GROUP_MAPPINGS.put(groupName, group); 
       } 
       return; 
      } 
     } 
     // group is not specified in the layout xml. Let's generate a random 
     // RadioGroup 
     SoftRadioGroup group = new SoftRadioGroup(); 
     group.addView(this); 
     Random rn = new Random(); 
     String groupName; 
     do { 
      groupName = Integer.toString(rn.nextInt()); 
     } while (GROUP_MAPPINGS.containsKey(groupName)); 
     GROUP_MAPPINGS.put(groupName, group); 
     mGroupName = groupName; 
     setOnClickListener(group); 

    } 

} 

SoftRadioGroup:

import java.util.ArrayList; 

import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.RadioButton; 

public class SoftRadioGroup implements OnClickListener { 

    private ArrayList<RadioButton> buttons = new ArrayList<RadioButton>(); 

    public void addView(RadioButton button) { 
     buttons.add(button); 
    } 

    @Override 
    public void onClick(View v) { 
     for (RadioButton button : buttons) { 
      button.setChecked(false); 
     } 
     RadioButton button = (RadioButton) v; 
     button.setChecked(true); 
    } 

    public RadioButton getCheckedRadioButton() { 
     for (RadioButton button : buttons) { 
      if (button.isSelected()) 
       return button; 
     } 
     return null; 
    } 

    public int getChildCount() { 
     return buttons.size(); 
    } 

    public RadioButton getChildAt(int i) { 
     return buttons.get(i); 
    } 

    public void check(SoftRadioButton button) { 
     if (buttons.contains(button)) { 
      for (RadioButton b : buttons) { 
       b.setChecked(false); 
      } 
     } 
    } 

} 

在嵌入在工作臺(2組,每組2個按鈕)XML佈局用法:

<TableLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:stretchColumns="1" > 

      <TableRow 
       android:id="@+id/tableRow1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 

       <Your.Package.SoftRadioButton 
        android:id="@+id/filterActivity_RadioButton_byDate" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:checked="true" 
        android:contentDescription="date" 
        android:text="@string/filterActivity_RadioButton_byDate" 
        fake:group="orderBy" /> 

       <Your.Package.SoftRadioButton 
        android:id="@+id/filterActivity_RadioButton_byPrice" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:contentDescription="price" 
        android:text="@string/filterActivity_RadioButton_byPrice" 
        fake:group="orderBy" /> 
      </TableRow> 

      <TableRow 
       android:id="@+id/tableRow2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 

       <Your.Package.SoftRadioButton 
        android:id="@+id/filterActivity_RadioButton_asc" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:contentDescription="down" 
        android:text="@string/filterActivity_RadioButton_asc" 
        fake:group="direction" /> 

       <Your.Package.SoftRadioButton 
        android:id="@+id/filterActivity_RadioButton_desc" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:checked="true" 
        android:contentDescription="up" 
        android:text="@string/filterActivity_RadioButton_desc" 
        fake:group="direction" /> 
      </TableRow> 
     </TableLayout> 
+0

輝煌!謝謝。 – Vyacheslav 2013-11-03 09:39:49

+0

警告:代碼會泄漏視圖。每次創建視圖時,它都會向靜態地圖添加所有SoftRadioButton視圖。用自定義視圖解決問題是一個好主意。我很好的解決方案可能是在視圖層次結構的根目錄添加ViewGroup,並讓它找到所有子視圖並將它們組合在一起。 – arberg 2016-06-08 14:29:59

+0

通過添加attr.xml修復屬性'group': ,並在xml-佈局行xmlns:fake =「http://schemas.android.com/apk/res-auto」 – arberg 2016-06-08 14:31:28

相關問題