2010-03-02 4 views
11

테이블을 사용하여 레이아웃을 지정하고 단일 라디오 그룹에 포함하려는 라디오 버튼이 여러 개 있습니다. 불행하게도 테이블 내부의 라디오 버튼은 그들이을 radioGroup 태그의 내부 이로 인해 당신은 둘 이상의 라디오 버튼을 선택할 수있어 사실을 무시하는 것테이블의 라디오 버튼에 라디오 그룹을 추가하는 방법은 무엇입니까?

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

그러나 : 나는 다음과 같은 XML 레이아웃을 시각. 테이블을 제거하고 라디오 버튼 만 있으면 잘 작동하는 것으로 나타났습니다. 어떻게 이것을 극복 할 수 있습니까? 라디오 그룹을 밖에서 대신 테이블 내부에 선언하는 것만 큼 간단할까요? 어떤 도움을 주셔서 감사합니다.

답변

7

RadioButton 위젯은 그룹 효과가 작동하려면 RadioGroup의 직계 하위이어야합니다.

+0

그래서 더있다 테이블로 버튼을 구성하고 라디오 그룹에 참여시키는 방법은? – Fizz

+0

'RadioButton' 위젯이'RadioGroup'의 직계 하위 항목 인 경우에만. 따라서,'RadioButton' 위젯은 테이블의 단일 셀에있는 행이나 열에 있어야합니다. – CommonsWare

+0

각 라디오 버튼을 프로그래밍 방식으로 그룹에 할당하면 어떨까요? – 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

답변으로 코드를 덤프하지 마십시오. 설명이 유용 할 것입니다. –

2

은 여기 내을 radioGroup/RadioButton 구성 확장 (SoftRadioGroup/SoftRadioButton)입니다. 레이아웃 XML에 RadioGroup이 더 이상 필요하지 않습니다. group이라는 속성으로 RadioButton을 그룹화 할 수 있습니다.

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

+0

경고 : 코드가보기를 유출합니다. 뷰가 생성 될 때마다 정적 맵에 모든 SoftRadioButton 뷰가 추가됩니다. 사용자 정의보기를 사용하여 문제를 해결하는 좋은 방법입니다. 좋은 해결책은 View 계층을 뷰 계층 구조의 루트 어딘가에 추가하여 모든 하위 뷰를 찾아서 그룹으로 묶을 수있게하는 것입니다. – arberg

+0

attr.xml을 추가하여 속성 'group'을 수정했습니다. 레이아웃 xmlns : fake = "http://schemas.android.com/apk/res-auto" – arberg

관련 문제