多选按钮CheckBox实例
来源:互联网 发布:按键精灵自动开枪源码 编辑:程序博客网 时间:2024/05/16 13:39
多选按钮(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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(cb1.isChecked()){tv.setText("你刚刚选择了"+ cb1.getText());sum++;}else{tv.setText("你刚刚取消了选择"+cb1.getText());sum--;}}}); cb2.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {if(cb2.isChecked()){tv.setText("你刚刚选择了"+ cb2.getText());sum++;}else{tv.setText("你刚刚取消了选择"+cb2.getText());sum--;}}}); cb3.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic 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() {@Overridepublic 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" ><TextViewandroid:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="你现在的目标有哪些?" /><CheckBoxandroid:id="@+id/cb1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="掌握android开发"/><CheckBoxandroid:id="@+id/cb2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="找到更好的工作"/><CheckBoxandroid:id="@+id/cb3"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="身体健康最重要~"/><Buttonandroid:id="@+id/bt"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="提交答案"/><TextViewandroid: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())
{
}
});
- 多选按钮CheckBox实例
- Android CheckBox 多选按钮
- Checkbox多选按钮的使用
- 12.3多选按钮(CheckBox)
- CheckBox 多选按钮的使用方法
- CheckBox多选按钮实现CompoundButton.OnCheckedChangeListener
- android多选按钮(CheckBox)
- Android之多选按钮CheckBox
- android sdk---checkBox多选实例
- android-UI组件实例大全(四)------单选按钮(RadioButton)和复选框(CheckBox)
- 多选按钮的实例
- 多选按钮的实例
- struts中获取多选按钮html:checkbox的值
- 从零开始学android<CheckBox多选按钮的使用.八.>
- Android_多选按钮(checkBox) 第一季重制版
- 玩转Android---UI篇---CheckBox(多选按钮)
- Android(5)多选按钮CheckBox、OnClickListener、OnCheckedChangeListener
- 多选按钮CheckBox及全选的实现
- 满汉楼2//model层
- tomcat6免配置的安装方法~
- Java_final关键字,不可变类,缓存不可变类实例(缓存池技术)
- 几个编译过程的概念
- 满汉楼3//db层
- 多选按钮CheckBox实例
- effective c++ 的50个最佳实践(三)
- JavaScript 开发进阶:理解 JavaScript 作用域和作用域链
- CSS中用背景图片做为超链接的方法
- 一虎一席谈 两部委严打会不会激化医患矛盾
- jdk 各个版本下载
- android基础(Activity)
- 7系列主板 规格对比
- WinForm程序中两份mdf文件问题的解决