How to uncheck a Checkbox when dialing another?

Asked

Viewed 1,371 times

3

I wish that couldn’t happen.

inserir a descrição da imagem aqui

The user clicking on the other CheckBox, disable the one previously marked.

Mainactivity.java

package genesysgeneration.umouoto;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

public class MainActivity extends AppCompatActivity {

    private CheckBox cb01, cb02;

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

        cb01=(CheckBox)findViewById(R.id.cb01);
        cb02=(CheckBox)findViewById(R.id.cb02);

        addCheckBoxChecked();

    }

    public void addCheckBoxChecked(){

        cb01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(((CheckBox)v).isChecked()){



                }

            }
        });

        cb02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



            }
        });

    }

}
  • It is not easier to use radiobutton no?

  • I thought about it and would put in an observation on the question. No, it does not suit me.

  • Because the radio button doesn’t fit?

  • I believe that the radiobutton can work with clusters (doubles, triples), but I intend to create activitys in which the conditions are not so simple, things that are not necessarily contrary, opposite, exclusionary. There will be cases where the user has to select for example 6 of 8 items.

  • But nothing prevents this case in specific implement radiobutton. And from what I can see, you will have to work with cross listeners between the two checkboxes to handle this, see this answer in Soen:http://stackoverflow.com/a/13133373/5524514

1 answer

6


The ideal is to use RadioButton and RadioGroup, however answering your question by using CheckBox, one of the ways is to do it this way:

cb01.setChecked(true);
cb01.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (cb01.isChecked())
            cb02.setChecked(false);
        else cb02.setChecked(true);
    }
});

cb02.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (cb02.isChecked())
            cb01.setChecked(false);
        else cb01.setChecked(true);
    }
});

Remembering that it can be written in other programming logic besides this.

As you did not specify very well the motif of being CheckBox, if it is a matter of layout, you can use the RadioButton and define how the CheckBox. This way, just add the custom style of the button in your style.xml:

<style name="Widget.CompoundButton.CheckBox"> 
    <item name="android:background">@android:drawable/btn_check_label_background</item>
    <item name="android:button">@android:drawable/btn_check</item>
</style>

Read more in the documentation:

  • 1

    THANK YOU VERY MUCH!!! I’ll take a look at this Radiobutton stop, but I think it really doesn’t suit me.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.