2011-11-25 3 views
1

나는 RadioGroup을 사용 중이며 RadioButton rdbut에서 RadioGroup rdgrp까지 rdgrp.addView(rdbut)을 추가했습니다.Android RadioGroup이 두 개 이상의 RadioButton을 확인합니까?

for(int j=0;j<3;j++) 
    { 
     RadioGroup rdgrp = new RadioGroup; 
     for(int i=0;i<=10;i++) 
     { 
      RadioButton rdbut = new RadioButton(this); 
      rdbut.setText("RadioButtion"+i); 
      rdbut.setId(i); 
      rdbut.setTag("somename"); 
      rdgrp.addView(rdbut); 
     } 
    }  

위 코드는 radiogroup 및 라디오 버튼을 초기화하는 방법을 보여줍니다. 에뮬레이터/모바일에서이 코드를 실행하면 한 번에 두 개의 라디오 버튼을 확인할 수 있습니다.

무엇이 문제 일 수 있습니까?

+0

11 개의 RadioButton으로 구성된 3 개의 그룹을 만들면 (이 경우는 어떻게됩니까?) 이제 모든 라디오 버튼이 나타나야합니다. 각 라디오 버튼 하나를 선택하여 한 번에 3 개의 라디오 버튼을 선택할 수 있습니다. –

+0

부모보기에'rdgrp'를 어떻게 추가합니까? 'RadioGroup'의 생성자도 이상합니다 (복사 붙여 넣기 중에는 오타가됩니다). –

+0

모든 rationgroup에서 두 개의 radiobutton을 선택할 수 있습니다. 그것은 6 개의 라디오 버튼을 의미하지만, 모든 라디오 그룹에서 2 개를 초과 할 수는 없습니다. – Raju

답변

1

이렇게 코드를 변경하십시오.

RadioGroup rdgrp[] = new RadioGroup[3]; 

    For(int j=0;j<3;j++) 
    { 
     RadioButton rdbut[] = new RadioButton[10]; 
     For(int i=0;i<=10;i++) 
     { 

      rdbut[i].setText("RadioButtion"+i); 
      rdbut[i].setId(j*100+i); 
      rdbut[i].setTag("somename"); 
      rdgrp[j].addView(rdbut[i]); 
     } 
    } 
+0

rdbut []은 RadioButton의 배열이며, 라디오 버튼 intances.u를 사용하여 어디에서나 초기화하지 않습니다. 편집 할 수 있습니다. –

0

당신은 세 가지 다른 라디오 그룹을 만들었습니다. 단일 그룹에서 하나의 라디오 버튼 만 선택할 수 있습니다. 세 그룹에서 세 개의 버튼을 선택할 수 있습니다.하지만 그룹 간 관계는 없습니다. 다른 그룹의 라디오 버튼을 동시에 선택할 수 있습니다. 귀하의 경우에는 최대 3 개의 버튼을 선택할 수 있습니다.

감사 선일

0

를 사용하여 사용자 레이아웃 파일이 XML 디자인 같은 일부 것.

<TableLayout 
      android:id="@+id/tbl_layoutchoice" 
      style="@style/InfoTableView" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="3dip" > 

      <RadioGroup 
       android:id="@+id/SelectLayout_Group" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" > 
      </RadioGroup> 
</TableLayout> 

및 사용에 대한 또 하나의 RadioGroup.Use에 라디오 버튼을 추가 할 필요 변화와 코드 아래 사용 그런 다음이

mRadioGroup = (RadioGroup) this.findViewById(R.id.SelectLayout_Group); 

와 같은 활동의 한 OnCreate() 방법 및 findView이을 radioGroup

라디오 버튼 생성에 필요한 선언 아래에 동적으로 있습니다.

 ArrayList<String> layoutlist = new ArrayList<String>(3); 
    int index = -1; 
    LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, 
     LayoutParams.WRAP_CONTENT); 


    for (String layout : layoutlist) { 
     RadioButton r = new RadioButton(this); 
     index++; 
     r.setText(layout); 
     r.setId(index); 
     r.setLayoutParams(lp); 
     r.setTextAppearance(this, R.style.TextBase); 


     mRadioGroup.addView(r); 


    } 

그래서 루프 전에 layoutlist에 문자열 값을 추가하는 것을 잊지 마세요 .and R.style는 일부의 RadioButton의 텍스트 표시에 대한 스타일을 요구합니다.

관련 문제