Android checkbox和radiobutton 以及Toast和AlertDialog的使用,androidcheckbox

來源:互聯網
上載者:User

Android checkbox和radiobutton 以及Toast和AlertDialog的使用,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 ArrayList<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 = "你沒有選擇選項";        }        new AlertDialog.Builder(this).setMessage(s).setPositiveButton("關閉",null).show();    }}


C#中的RadioButton與CheckBox的使用

你用的方法不合理,如果第1個和第2個同時被選中的話代碼只會執行
if(this.checkBox1.Checked)
{
MessageBox.Show("一選中 二將選");
this.checkBox2.CheckState = CheckState.Checked;
return;
}
這就是為什麼不管如何點擊,第三個不會被選中的原因。
改成:
private void button2_Click(object sender, System.EventArgs e)
{
if(this.checkBox1.Checked && (!this.checkBox2.Checked))
{
MessageBox.Show("一選中 二將選");
this.checkBox2.CheckState = CheckState.Checked;
return;
}
if(this.checkBox2.Checked(!this.checkBox3.Checked))
//else
{
MessageBox.Show("二選中 三將選");
this.checkBox3.CheckState = CheckState.Checked;
return;
}
if(this.checkBox3.Checked)
{
MessageBox.Show("三選中 四將選");
this.checkBox4.CheckState = CheckState.Checked;
return;
}
this.checkBox1.CheckState = CheckState.Checked;
}
 
C#的RadioButton與CheckBox有何不同


你用的方法不合理,如果第1個和第2個同時被選中的話代碼只會執行
if(this.checkBox1.Checked)
{
MessageBox.Show("一選中 二將選");
this.checkBox2.CheckState = CheckState.Checked;
return;
}
這就是為什麼不管如何點擊,第三個不會被選中的原因。
改成:
private void button2_Click(object sender, System.EventArgs e)
{
if(this.checkBox1.Checked && (!this.checkBox2.Checked))
{
MessageBox.Show("一選中 二將選");
this.checkBox2.CheckState = CheckState.Checked;
return;
}
if(this.checkBox2.Checked(!this.checkBox3.Checked))
//else
{
MessageBox.Show("二選中 三將選");
this.checkBox3.CheckState = CheckState.Checked;
return;
}
if(this.checkBox3.Checked)
{
MessageBox.Show("三選中 四將選");
this.checkBox4.CheckState = CheckState.Checked;
return;
}
this.checkBox1.CheckState = CheckState.Checked;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.