2012-05-03 8 views
3

나는 Android에 라디오 버튼 그룹을 가지고 있습니다. 항목을 선택하면 이벤트가 수신됩니다. 지금까지의 정상. 하지만 사용자가 이미 선택한 항목을 클릭하면 이벤트가 표시되지 않습니다. 사용자가 라디오 버튼을 선택했는지 여부를 알 수있는 방법이 있습니까 (이벤트 수신)? 고마워요. 귀하의 버튼에 OnClickListener()을 설정하여라디오 버튼 클릭 & reclick

답변

3

당신이로 라디오 버튼의 선택을 취소하려면 내가 이미 확인 라디오 버튼, 를 클릭 할 때 이벤트를 얻을 것이라고 이유를 이해하지 않지만 이미 선택되어 있다면 클릭하십시오 !! 이 코드를 확인하면 도움이 될 수 있습니다 :

RadioGroup radioGroup; 
RadioButton radioButton1; 
RadioButton radioButton2; 
RadioButton radioButton3; 

boolean hack = false; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    radioGroup = (RadioGroup) findViewById(R.id.rg); 
    radioButton1 = (RadioButton) findViewById(R.id.r1); 
    radioButton2 = (RadioButton) findViewById(R.id.r2); 
    radioButton3 = (RadioButton) findViewById(R.id.r3); 

    OnClickListener radioClickListener = new OnClickListener() 
    { 

     public void onClick(View v) 
     {  //The first codition check if we have clicked on an already selected radioButton 
      if (v.getId() == radioGroup.getCheckedRadioButtonId() && hack) 
      { 
       radioGroup.clearCheck(); 
      } 
      else 
      { 
       hack = true; 
      } 
     } 
    }; 

    OnCheckedChangeListener radioCheckChangeListener = new OnCheckedChangeListener() 
    { 

     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      hack = false; 
     } 
    }; 

    radioButton1.setOnCheckedChangeListener(radioCheckChangeListener); 
    radioButton2.setOnCheckedChangeListener(radioCheckChangeListener); 
    radioButton3.setOnCheckedChangeListener(radioCheckChangeListener); 

    radioButton1.setOnClickListener(radioClickListener); 
    radioButton2.setOnClickListener(radioClickListener); 
    radioButton3.setOnClickListener(radioClickListener); 

} 
Hope this wil help 
+0

내 경우에는 항목이 이미 선택되어있는 활동 이벤트를 시작하려고하기 때문에 콜백을 받고 싶습니다. –