I have a checkbox set but only wanted one selected

1

I have a set of checkbox but only wanted one selected and, if they selected another, the one that was selected was deselected.

I've tried this, but it does not work:

if (chkfemenino.isChecked()) {
    params.add(new BasicNameValuePair("genero", generofem));
    chkmasculino.setChecked(false);         
}
if (chkmasculino.isChecked()) {
    params.add(new BasicNameValuePair("genero", generomasc));
    chkfemenino.setChecked(false);          
}
    
asked by anonymous 28.04.2015 / 12:58

1 answer

4

It's simple to unselect a CheckBox when you select the other:

chkfemenino.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    {
        if (buttonView.isChecked()) 
        {
            ischkfeminino  = isChecked;
            ischkmasculino = false;

            chkfeminino. setChecked(ischkfeminino );
            chkmasculino.setChecked(ischkmasculino);
        }
    }
});

And you do the same for chkmasculino .

    
28.04.2015 / 13:19