Two checkboxes, only one can be selected

I work on the login screen, where basically you can choose whether you want to have a 4-digit PIN access or a secure cleaning method; the way I want it to look is that there are two checkboxes in the layout, initially they are both unchecked. When you select one, it is checked, but if you try to select another, it will automatically turn off the first. I have two checkboxes defined in the layout, but I'm a little confused by the fact that I would have to refer to force the uncheck.

public void onCheckBoxClicked(View view){ boolean checked = ((CheckBox)view).isChecked(); switch(view.getId()){ case R.id.setupCheckBox1: if(checked){ } 

Basically, I know that something should go into this if {} is an expression that cancels setupCheckBox2 automatically, but I just don't know what should be made false. Any help appreciated!

In addition, I understand that this is a kind of one-line answer, if someone has good links to checkboxes in Android, I would also like to read about it. I'm a little new to this.

+4
source share
3 answers

use this code and look at the selected ckeck field value in logcat .....

  public class MainActivity extends Activity implements OnClickListener { CheckBox hindi, english; Button submit; String language; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); hindi = (CheckBox) findViewById(R.id.checkBox1); english = (CheckBox) findViewById(R.id.checkBox2); submit = (Button) findViewById(R.id.submit); submit.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.submit: if(hindi.isChecked()) { language="hindi" ; } if(english.isChecked()) { language="english" ; } if((hindi.isChecked()==true)&&(english.isChecked()==true)) { Toast.makeText(DrivingTestActivity.this, "please select one language", Toast.LENGTH_LONG).show(); } else { System.out.println("language" + language); } break; default: break; } } } 
0
source

It is very simple, try this code.

 public onCreate(Bundle savedInstanceState) { cbx_hindi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { cbx_english.setChecked(false); } } }); cbx_english.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { cbx_hindi.setChecked(false); } } }); } 
0
source

Source: https://habr.com/ru/post/1485053/


All Articles