android CheckBox使用和状态获得
2015-12-02 07:46
495 查看
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical"> <!-- 定义CheckBox控件 ,代表篮球选项--> <CheckBox android:id="@+id/CbBasketball" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="篮球" /> <!-- 定义CheckBox控件 ,代表乒乓球选项--> <CheckBox android:id="@+id/CbPingpangball" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="乒乓球" /> <!-- 定义CheckBox控件 ,代表足球选项--> <CheckBox android:id="@+id/CbFootball" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="足球" /> <!-- 定义TextView控件,来显示选中结果 --> <TextView android:id="@+id/TvResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/str" /> </LinearLayout>
package com.example.yanlei.yl2; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; //导入必备的包 import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends AppCompatActivity { private CheckBox CbBasketball; //定义篮球的复选框对象 private CheckBox CbPingpangball; //定义乒乓球的复选框对象 private CheckBox CbFootball; //定义足球的复选框对象 private TextView TvResult; //定义结果文本便签对象 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //调用父类的onCreate方法 //通过setContentView方法设置当前页面的布局文件为activity_main setContentView(R.layout.activity_main); findView(); //获取页面中的控件 setListener(); //设置控件的监听器 } private void setListener() { // TODO Auto-generated method stub //设置所有CheckBox的状态改变监听器 CbBasketball.setOnCheckedChangeListener(myCheckChangelistener); CbPingpangball.setOnCheckedChangeListener(myCheckChangelistener); CbFootball.setOnCheckedChangeListener(myCheckChangelistener); } OnCheckedChangeListener myCheckChangelistener = new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub //设置TextView的内容显示CheckBox的选择结果 setText(); } }; private void findView() { // TODO Auto-generated method stub //通过findViewById得到对应的控件对象 CbBasketball = (CheckBox)findViewById(R.id.CbBasketball); CbPingpangball = (CheckBox)findViewById(R.id.CbPingpangball); CbFootball = (CheckBox)findViewById(R.id.CbFootball); TvResult = (TextView)findViewById(R.id.TvResult); } private void setText(){ String str; TvResult.setText(""); //清空TextView的内容 //如果CbBasketball被选中,则加入TvResult内容显示 if (CbBasketball.isChecked()) { str = TvResult.getText().toString()+CbBasketball.getText().toString()+","; TvResult.setText(str); } //如果CbPingpangball被选中,则加入TvResult内容显示 if (CbPingpangball.isChecked()) { str = TvResult.getText().toString()+CbPingpangball.getText().toString()+","; TvResult.setText(str); } //如果CbFootball被选中,则加入TvResult内容显示 if (CbFootball.isChecked()) { str = TvResult.getText().toString()+CbFootball.getText().toString(); TvResult.setText(str); } } }
相关文章推荐
- android中setClickable,setEnabled,setFocusable的含义及区别
- android学习笔记:Intent,Intent Filter
- Android实现推送方式解决方案
- 安卓开发学习笔记(二)
- android Media Router
- 野人学Android第二弹——照片的拍摄与存储(二)
- 什么时候会启动多个进程
- Android Studio安装
- java web与android互通的aes算法
- Android Activity的启动方式
- Android 广播的发送与接收
- Android中Js与java的交互
- Android 自定义适配器
- 选蜂窝教育Android培训 高薪入职安卓工程师
- 用版本控制系统来管理android studio项目时 配置忽略文件
- android-通过StatFs获取文件系统的空间信息
- Android Intent.FLAG_NEW_TASK详解,包括其他的标记的一些解释
- (写笔记习惯)关于从eclipse导入android studio 中,代码乱码的情况
- 将Android下的可执行文件以静态库的形式进行编译
- Android-Property Animation(属性动画)