Android控件之CheckBox多选框
2015-10-27 19:00
489 查看
Android中的CheckBox控件既可以通过Button按钮来监听其选中状态,它也有自己的事件处理方法,通过一个小例子来看具体代码
如下图:
![](http://img.blog.csdn.net/20151027185347779)
![](http://img.blog.csdn.net/20151027185713642)
1.0 activity_main.xml:
2.0 MainActivity:
如下图:
1.0 activity_main.xml:
<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:orientation="horizontal" android:gravity="center" tools:context="com.lgl.checkbox.MainActivity" > <CheckBox android:id="@+id/cb01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/huawei" /> <CheckBox android:id="@+id/cb02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/iphone" /> <CheckBox android:id="@+id/cb03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/xiaomi" /> <Button android:id="@+id/btn01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/click" /> </LinearLayout>
2.0 MainActivity:
package com.lgl.checkbox; 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.Toast; public class MainActivity extends Activity { private CheckBox ch01; private CheckBox ch02; private CheckBox ch03; private Button btn01; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /** * 获取控件 */ ch01 = (CheckBox) findViewById(R.id.cb01); ch02 = (CheckBox) findViewById(R.id.cb02); ch03 = (CheckBox) findViewById(R.id.cb03); btn01 = (Button) findViewById(R.id.btn01); /** * 为三个CheckBox分别绑定监听 */ ch01.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { String text = ch01.getText().toString(); Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); } }); ch02.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { String text = ch02.getText().toString(); Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); } }); ch03.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { String text = ch03.getText().toString(); Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); } }); /** * 为Button绑定监听 */ btn01.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String text = "您选择了:"; if(ch01.isChecked()) { text += ch01.getText().toString(); } if(ch02.isChecked()) { text += ch02.getText().toString(); } if(ch03.isChecked()) { text += ch03.getText().toString(); } Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show(); } }); } }
相关文章推荐
- Android进阶之路——AIDL
- android里面隐藏控件的方法
- Android引入高速缓存的异步加载全分辨率
- Android 基础控件 TextView
- Android adb.exe程序启动不起来,如何处理
- android listview 嵌套ListView,子lv高度的问题
- Android WiFi移植
- 【Android 基础】Animation 动画介绍和实现
- Android自定义可循环的滚动选择器CycleWheelView 替代TimePicker/NumberPicker/WheelView
- Android Activity界面跳转讲解
- 自定义可移动view
- Android开发中的多线程
- Android框架之EventBus的简单使用
- Android-Universal-Image-Loader三大组件DisplayImageOptions、ImageLoader、ImageLoaderConfiguration详解
- 【转】android:layout_gravity和android:gravity的区别
- Android传感器框架分析
- Android基础之AutoCompleteTextView的使用
- Android Add外部library工程,总是链接失败的原因
- 解决 Gradle DSL method not found: 'android()'
- android studio 怎么回到欢迎界面