Android checkbox, radiobutton, Toast, and AlertDialog, and androidcheckbox
package com.example.radiobutton_01;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MyActivity extends Activity { private RadioGroup rg; private Button btn; /** * Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rg = (RadioGroup)findViewById(R.id.rg); btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int len = rg.getChildCount(); String msg = ""; for(int i=0;i<len;i++) { RadioButton rb = (RadioButton)rg.getChildAt(i); if(rb.isChecked()) { msg = rb.getText().toString(); break; } } Toast.makeText(MyActivity.this,msg,Toast.LENGTH_SHORT).show(); } }); }}
Package com. example. checkbox_01; import android. app. activity; import android. app. alertDialog; import android. OS. bundle; import android. view. view; import android. widget. button; import android. widget. checkBox; import android. widget. linearLayout; import java. util. arrayList; import java. util. list; public class MyActivity extends Activity implements View. onClickListener {private List <CheckBox> checkBoxes = new Ar RayList <CheckBox> ();/*** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // setContentView (R. layout. main); String [] checkBoxText = new String [] {"are you student? "," Are you love android? "," Are you dev? "}; LinearLayout linearLayout = (LinearLayout) getLayoutInflater (). inflate (R. layout. main, null); for (int I = 0; I <checkBoxText. length; I ++) {CheckBox checkBox = (CheckBox) getLayoutInflater (). inflate (R. layout. checkbox, null); checkBox. setText (checkBoxText [I]); checkBoxes. add (checkBox); linearLayout. addView (checkBox, I);} setContentView (linearLayout); Button btn = (Button) findViewById (R. id. btn); btn. setOnClickListener (this) ;}@ Override public void onClick (View view) {String s = ""; for (CheckBox cb: checkBoxes) {if (cb. isChecked () {s + = cb. getText () + "\ n" ;}} if ("". equals (s) {s = "You have not selected the option";} new AlertDialog. builder (this ). setMessage (s ). setPositiveButton ("off", null ). show ();}}
Use of RadioButton and CheckBox in C #
The method you use is unreasonable. If 1st and 2nd are selected at the same time, the Code will only execute
If (this. checkBox1.Checked)
{
MessageBox. Show ("select one or two ");
This. checkBox2.CheckState = CheckState. Checked;
Return;
}
This is why the third option is not selected no matter how you click it.
Changed:
Private void button2_Click (object sender, System. EventArgs e)
{
If (this. checkBox1.Checked &&(! This. checkBox2.Checked ))
{
MessageBox. Show ("select one or two ");
This. checkBox2.CheckState = CheckState. Checked;
Return;
}
If (this. checkBox2.Checked (! This. checkBox3.Checked ))
// Else
{
MessageBox. Show ("select two, select three ");
This. checkBox3.CheckState = CheckState. Checked;
Return;
}
If (this. checkBox3.Checked)
{
MessageBox. Show ("Three select four ");
This. checkBox4.CheckState = CheckState. Checked;
Return;
}
This. checkBox1.CheckState = CheckState. Checked;
}
What is the difference between C # RadioButton and CheckBox?
The method you use is unreasonable. If 1st and 2nd are selected at the same time, the Code will only execute
If (this. checkBox1.Checked)
{
MessageBox. Show ("select one or two ");
This. checkBox2.CheckState = CheckState. Checked;
Return;
}
This is why the third option is not selected no matter how you click it.
Changed:
Private void button2_Click (object sender, System. EventArgs e)
{
If (this. checkBox1.Checked &&(! This. checkBox2.Checked ))
{
MessageBox. Show ("select one or two ");
This. checkBox2.CheckState = CheckState. Checked;
Return;
}
If (this. checkBox2.Checked (! This. checkBox3.Checked ))
// Else
{
MessageBox. Show ("select two, select three ");
This. checkBox3.CheckState = CheckState. Checked;
Return;
}
If (this. checkBox3.Checked)
{
MessageBox. Show ("Three select four ");
This. checkBox4.CheckState = CheckState. Checked;
Return;
}
This. checkBox1.CheckState = CheckState. Checked;
}