2012-03-11 4 views
1

두 번째 라디오 버튼이 끈적 거리는 것을 제외하면 라디오 버튼이 잘 작동합니다. 다른 것들은 선택되어 토글되지만, 디스플레이는 두 번째 버튼이 고정되어 있음을 보여줍니다.안드로이드의 끈적한 라디오 버튼

아래의 자체 코드 예제를 참조하십시오. 나는 4.0.3을 사용하고 있고 이클립스를 사용하여 에뮬레이터에서 실행 중이다.

감사

package small.example; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.*; 
public class SmallActivity extends Activity { 
    @Override public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     LinearLayout main=new LinearLayout(this); 
     main.setOrientation(LinearLayout.VERTICAL); 
     int question=1; 
     button=new Button(this); 
     button.setText("Make a choice"); 
     main.addView(button); 
     button.setId(question); 
     setContentView(main); 
     int answers=4; 
     radioButtons=new RadioButton[answers]; 
     RadioGroup radioGroup=addRadioButtonsToGroup(question,radioButtons); 
     LinearLayout linearLayout=new LinearLayout(this); 
     linearLayout.addView(radioGroup); 
     linearLayout.setId(question); 
     // radioButtons[0].setChecked(true); 
     main.addView(linearLayout); 
    } 
    private RadioGroup addRadioButtonsToGroup(int question,RadioButton[] radioButtons) { 
     RadioGroup radioGroup=new RadioGroup(this); 
     for(int i=0;i<radioButtons.length;i++) { 
      radioButtons[i]=new RadioButton(this); 
      radioButtons[i].setText("Question "+question+" Choice "+(i+1)); 
      radioButtons[i].setId(i); 
      radioGroup.addView(radioButtons[i]); 
     } 
     radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener); 
     radioGroup.setId(question); 
     return radioGroup; 
    } 
    RadioButton[] radioButtons; 
    Button button; 
    RadioGroup.OnCheckedChangeListener radioGroupOnCheckedChangeListener=new RadioGroup.OnCheckedChangeListener(){ 
     public void onCheckedChanged(RadioGroup group,int checkedId) { 
      int id=checkedId; 
      button.setText(radioButtons[id].getText()); 
     } 
    }; 
} 

답변

0
잘못된 버튼 주가는 라인

radioGroup.setId(question); 

에 의한을 radioGroup의 ID를 가진 ID가, 왜이 버튼 스티커 ISN 원인이 될 것을 보인다

' 나에게 분명히해라. 해당 행을

radioGroup.setId(radioGroup.getChildCount()); 

으로 변경하면 충돌하지 않는 ID가되므로 모든 버튼이 정상적으로 작동합니다.

+0

위대한! - 매력처럼 작동합니다. 덕분에 –

+0

도와 줘서 기뻐요. 좋은 질문, 자그마한, 자체 포함 된 예제 - 잘라내어 붙여넣고 문제를보기 위해 실행하십시오. 더 많은 포스터 만이 같은 양의 문제를 해결하면. – NickT

관련 문제