2.常用控件:CheckBox
来源:互联网 发布:950 600淘宝轮播图 编辑:程序博客网 时间:2024/06/06 02:17
常用控件:CheckBox
核心代码:
视图:
<CheckBoxandroid:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swim" /><CheckBoxandroid:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/run" /><CheckBoxandroid:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read" />
数据:
//对控件对象进行声明private CheckBox swimBox = null;private CheckBox runBox = null;private CheckBox readBox = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radio); //通过控件的ID来得到代表控件的对象 swimBox = (CheckBox)findViewById(R.id.swim); runBox = (CheckBox)findViewById(R.id.run); readBox = (CheckBox)findViewById(R.id.read); //为多选按钮添加监听器 swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("swim is checked");}else{System.out.println("swim is unchecked");}}}); runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("run is checked");}else{System.out.println("run is unchecked");}}}); readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("read is checked");}else{System.out.println("read is unchecked");}}});
控件图例:
具体应用:
XML文件:
<?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" ><CheckBoxandroid:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swim" /><CheckBoxandroid:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/run" /><CheckBoxandroid:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read" /></LinearLayout>
Activity.java文件:
package mars.activity07;import android.app.Activity;import android.os.Bundle;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class RadioTest extends Activity { /** Called when the activity is first created. *///对控件对象进行声明private CheckBox swimBox = null;private CheckBox runBox = null;private CheckBox readBox = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.radio); //通过控件的ID来得到代表控件的对象 swimBox = (CheckBox)findViewById(R.id.swim); runBox = (CheckBox)findViewById(R.id.run); readBox = (CheckBox)findViewById(R.id.read); //为多选按钮添加监听器 swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("swim is checked");}else{System.out.println("swim is unchecked");}}}); runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("run is checked");}else{System.out.println("run is unchecked");}}}); readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubif(isChecked){System.out.println("read is checked");}else{System.out.println("read is unchecked");}}}); } }
项目文件地址:
阅读全文
0 0
- 2.常用控件:CheckBox
- 常用控件RadioButton/CheckBox/Toast
- android常用控件------checkBox RadioButton ImageView Spinner
- android 常用控件之 (CheckBox)使用
- CheckBox控件
- CheckBox控件
- checkbox控件
- CheckBox控件
- CheckBox控件
- CheckBox控件
- CheckBox 控件
- Android常用控件(一)【RadioGroup、RadioButton、CheckBox、Toast】
- Android 常用控件自定义样式RadioButton、CheckBox、ProgressBar、
- LibGDX_6.4: 常用系统控件: 复选框(CheckBox)
- Android(六) 常用控件(二)单选框,toast,checkbox,progressbar,listview
- [android]控件CheckBox/RadioGroup/RadioButton的常用属性---单复选控件
- CheckBox控件和CheckBoxList控件
- Windows 窗体 CheckBox 控件
- python 多维数组的排序
- ExcelUtil.java
- [ProtoBuf]ProtoBuf原理
- ExcelUtil.java
- Linux下的rsync命令
- 2.常用控件:CheckBox
- eclipse
- 利用Bmob + ImageLoader 加载网络图片
- nginx: [error] invalid PID number "" in "/run/nginx.pid"
- Android基础_创建自定义xml图片(二)
- Sum Root to Leaf Numbers问题及解法
- qrcode生成二维码
- 之前项目好好的,为啥bootstrap的小图标突然不显示了,样式不起作用
- Eclipase报错8005,8009,8080端口号被占用,无法run项目