2010-03-02 20 views
11

Bir tablo kullanarak yerleştirmek istediğim birden çok radyo düğmem var, ancak bunları tek bir radyo grubunda da dahil et. maalesef tablonun içini radyo düğmeleri, RadioGroup etiketlerinin içinde ve bu nedenle size birden fazla radyo düğmesini seçebilirsiniz olduğun gerçeğini göz ardı etmek gibiBir masanın içindeki radyo düğmelerine radyo grubu nasıl eklenir?

<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> 

Ama: Aşağıdaki xml düzeni zaman. Tabloyu kaldırarak ve sadece radyo düğmelerine sahip olmanın çok iyi çalıştığını fark ettim. Bunu nasıl aşabilirim? Radyo grubunu dışarıdaki masanın içinde ilan etmek kadar basit olur mu? Herhangi bir yardım için teşekkürler.

cevap

7

Sizin RadioButton widget grup etkisi çalışması için RadioGroup derhal çocukları olmalıdır.

+0

Yani orada Düğmeleri bir masa ile yapılandırmanın ve onları bir radyo grubunun parçası haline getirmenin yolu nedir? – Fizz

+0

Sadece “RadioButton” widget'ları “RadioGroup” un hemen çocuklarıysa. Bu nedenle, 'RadioButton' widget'larınızın tablonuzun tek bir hücresinde bir satır veya sütunda olması gerekir. – CommonsWare

+0

Grup halinde olmak için her radyo düğmesini programlı olarak atarsam ne olur? – nobalG

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

Lütfen sadece cevabınızı kod olarak dökmeyin. Bir açıklama yararlı olacaktır. –

2

İşte RadioGroup/RadioButton uzantım (SoftRadioGroup/SoftRadioButton). RadioGroup, XML düzeninde artık gerekli değildir. RadioButtons'u grup adı verilen bir özellik ile gruplayabilirsiniz.

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); 
      } 
     } 
    } 

} 

(grup başına 2 düğmeli 2 grupları) bir tablo gömülü XML düzeninde Kullanımı:

<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

parlak! Teşekkürler. – Vyacheslav

+0

Uyarı: kod görünümleri sızdırıyor. Görünümler her oluşturulduğunda statik haritaya tüm SoftRadioButton görünümlerini ekleyecektir. Sorunu özel bir görünümle çözmek için harika bir fikir. İyi bir çözüm, ViewGroup'u görünüm hiyerarşisinin kökünde bir yere eklemek ve tüm çocuk görüşlerini bulup gruplara ayırmak olabilir. – arberg

+0

Fix nitelik attr.xml ekleyerek 'grubu': <özn name = "grubu" format = "string" /> ve, XML ithalat hat xmlns düzeni: sahte = "http://schemas.android.com/apk/res-auto" hafıza-sızıntısı için bir QuickFix – arberg