Android UI控件详解-CheckBox(多选框)

来源:互联网 发布:手机如何申请淘宝直播 编辑:程序博客网 时间:2024/05/01 11:03
package com.example.duoxuan;import android.os.Bundle;import android.app.Activity;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.Toast;/** *  * @author TXF *  * 多选框 * 还有一种相对简单的方法来实现,这里就不再写了。 *  */public class MainActivity extends Activity {private CheckBox mcb1, mcb2, mcb3, mcb4;private Button mbtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mcb1 = (CheckBox) findViewById(R.id.cb_0);mcb2 = (CheckBox) findViewById(R.id.cb_1);mcb3 = (CheckBox) findViewById(R.id.cb_2);mcb4 = (CheckBox) findViewById(R.id.cb_3);mbtn = (Button) findViewById(R.id.submit);// 设置被选择监听器,这个监听器和RadioGroup的监听器是一样的mcb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (mcb1.isChecked()) {displayToast("你选择了" + mcb1.getText());}}});mcb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (mcb2.isChecked()) {displayToast("你选择了" + mcb2.getText());}}});mcb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (mcb3.isChecked()) {displayToast("你选择了" + mcb3.getText());}}});mcb4.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (mcb4.isChecked()) {displayToast("你选择了" + mcb4.getText());}}});mbtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {int num = 0;if (mcb1.isChecked()) {num++;}if (mcb2.isChecked()) {num++;}if (mcb3.isChecked()) {num++;}if (mcb4.isChecked()) {num++;}displayToast("谢谢诶参与,您一共选择了" + num + "项");}});}private void displayToast(String str) {Toast toast = Toast.makeText(this, str, 0);toast.setGravity(Gravity.CENTER, 0, 0);toast.show();}}

xml布局

   <TextView        android:id="@+id/tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:text="你的爱好是什么"        android:textSize="20sp" />    <CheckBox        android:id="@+id/cb_0"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/tv"        android:checked="true"        android:text="听歌" />    <CheckBox        android:id="@+id/cb_1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/cb_0"        android:text="跳舞" />    <CheckBox        android:id="@+id/cb_2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/cb_1"        android:text="上网" />    <CheckBox        android:id="@+id/cb_3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/cb_2"        android:text="睡觉" />    <Button        android:id="@+id/submit"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_below="@+id/cb_3"        android:text="提交" />
效果图


0 0
原创粉丝点击