Click and re-click

I have a switch group on Android. I get events when items are selected. Still. But I do not receive an event if the user clicks on an already selected item. Is there a way to find out (receive an event) when users click on a radio cassette if it is selected or not? Many thanks.

+3
source share
2 answers

I don’t understand why you received the event when you click on the already checked switch, but if you want to deselect the radio button by clicking on it, if it is already selected! check this code which it can help you:

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
+3
source

By installing OnClickListener()on your buttons ...

0
source

All Articles