Android控件笔记——CheckBox复选框
2016-05-13 00:00
585 查看
1、CheckBox的两种状态:
选中状态(true),未选中状态(false);
2、属性:
android:id="@+id/checkbox"
android:layout_width="warp_content"
android:layout_height="warp_content"
android:checked="false"
android:text="男"
3、应用:
拖入CheckBox控件,并对它进行相关设置:
初始化CheckBox,并设置监听器监听他的状态,并根据对应状态输出提示信息:
4、效果:
![](http://static.oschina.net/uploads/space/2016/0513/012722_2rFQ_2725918.png)
选中状态(true),未选中状态(false);
2、属性:
android:id="@+id/checkbox"
android:layout_width="warp_content"
android:layout_height="warp_content"
android:checked="false"
android:text="男"
3、应用:
拖入CheckBox控件,并对它进行相关设置:
[code=plain]<!--activity_main.xml--> <CheckBox android:checked="false" android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你是猪" />
初始化CheckBox,并设置监听器监听他的状态,并根据对应状态输出提示信息:
[code=plain] public class MainActivity extends Activity { //定义checkBox private CheckBox checkBox; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化checkBox checkBox=(CheckBox) findViewById(R.id.checkBox1); //通过设置checkBox的监听事件来对checkBox是不是被选中 checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { //通过匿名内部类来实现监听器 @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub //通过onCheckChanged来监听当前的checkBox是否被选中 if(arg1){ //获得checkBox的文本内容 String text="对!"+checkBox.getText().toString(); Toast.makeText(MainActivity.this, text, 1).show(); } else{ Toast.makeText(MainActivity.this, "不对,你是猪!", 1).show(); } } }); } }
4、效果:
![](http://static.oschina.net/uploads/space/2016/0513/012722_2rFQ_2725918.png)
![](http://static.oschina.net/uploads/space/2016/0513/012739_crYf_2725918.png)
相关文章推荐
- Android控件笔记——使用RadioGroup和RadioButton实现单选效果
- Android中文API:Android 如何保持屏幕长亮?
- 你真的会用Android中Strings资源吗
- 【自用】Android 改变Bitmap图片资源为指定大小
- Android中用到的设计模式
- AndroidStudio降低编译版本报错
- Android基础——四大组件之Activity
- Android RGB颜色查询对照表
- Android进程间通信(IPC)之Messenger
- Android stadio 调试太掉了
- Android 实现拍照功能
- android ScrollView触摸点源码解读
- 加速Android Studio---Gradle构建
- RecyclerView检测是否滑动到底部
- Android四大组件之Service(服务)简单总结
- Android之PendingIntent
- Android基础之测量text宽度的三种方式
- 在Android studio IDE中采用自己编写的Android.mk
- Android 引入aar相关问题
- Activity四种启动模式