多选按钮CheckBox实例

本文详细介绍了在Android中实现多选按钮CheckBox的实例,包括Java代码实现、布局文件设置,以及运行结果的展示。同时,文章还讨论了如何添加多选框的监听方法,以实现特定操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多选按钮(CheckBox)实例

java代码

package com.pms.mycheckbox;

import android.app.Activity;
import android.os.Bundle;
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.TextView;

public class MyCheckBox extends Activity {
	
	private CheckBox cb1;
	private CheckBox cb2;
	private CheckBox cb3;
	private TextView tv;  //用来显示题目及用户的选择情况
	private Button   bt;
	private TextView tv_result;
	int sum = 0;          //用来对勾选的项目计数
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        /*得到各个控件的引用*/
        cb1 = (CheckBox) findViewById(R.id.cb1);
        cb2 = (CheckBox) findViewById(R.id.cb2);
        cb3 = (CheckBox) findViewById(R.id.cb3);
        tv  = (TextView) findViewById(R.id.tv);
        bt = (Button) findViewById(R.id.bt);
        tv_result = (TextView) findViewById(R.id.tvresult);
        
        /*为每个CheckBox绑定监听器来获得选择情况,根据选择情况设置显示文本内容,并且为选中的计数*/
        cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(cb1.isChecked())
				{
					tv.setText("你刚刚选择了"+ cb1.getText());
					sum++;
				}
				else
				{
					tv.setText("你刚刚取消了选择"+cb1.getText());
					sum--;
				}
				
			}
		});
        
        cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(cb2.isChecked())
				{
					tv.setText("你刚刚选择了"+ cb2.getText());
					sum++;
				}
				else
				{
					tv.setText("你刚刚取消了选择"+cb2.getText());
					sum--;
				}
				
			}
		});
        
        cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(cb3.isChecked())
				{
					tv.setText("你刚刚选择了"+ cb3.getText());
					sum++;
				}
				else
				{
					tv.setText("你刚刚取消了选择"+cb3.getText());
					sum--;
				}
				
			}
		});
        
        /*为Button绑定监听器*/
        bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String result = "";  //用来存放结果
				/*取出选中的内容,换行显示*/
				if(cb1.isChecked()) result += cb1.getText() + "\n";
				if(cb2.isChecked()) result += cb2.getText() + "\n";
				if(cb3.isChecked()) result += cb3.getText() + "\n";
				if(result!="")//判断是否为空值
				{
					tv_result.setText("你的目标是:"  + "\n" + result + "\n" + "你一共选择了" + sum + "个选项");
				}
				else
				{
					tv_result.setText("选择不能为空!年轻人要有点追求啊!");
				}
			}
		});
        
    }
}


布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
	android:id="@+id/tv"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="你现在的目标有哪些?"
    />
<CheckBox
	android:id="@+id/cb1"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="掌握android开发"
	/>
<CheckBox
	android:id="@+id/cb2"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="找到更好的工作"
	/>
<CheckBox
	android:id="@+id/cb3"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	android:text="身体健康最重要~"
	/>
<Button
	android:id="@+id/bt"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="提交答案"
	/>
<TextView
	android:id="@+id/tvresult"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	/>
</LinearLayout>

运行结果

总结

多选框的监听方法

 cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
   
   @Override
   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(cb1.isChecked())
    {
     }
  });

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值