2010-12-08 15 views

Répondre

22
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) 
     { 
      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); 

} 

Ok maintenant je l'ai mis à jour. Cela devrait fonctionner Philipz

+0

Très bon homme, mauvais tester plus tard, vraiment apprécier votre réponse, merci! –

+0

Hé mec, il ne vérifie jamais le RadioButton, car il est en train de vérifier et après cette décocher, donc l'état final est décoché! :/ –

+0

Oui Philipz l'a testé et trouvé comme vous l'avez dit. Cependant, j'ai réussi à le faire fonctionner avec l'aide d'un booléen .. mettra à jour la réponse dans quelques instants. – Varun