CheckBox是多选方框,具有选中和未选中两种状态,他不像单选按钮在一个组中只能选择一个,而checkBox我们一次可以选中多个多选按钮!

CheckBox相应的属性及方法:

默认选中:android:checked="true";可以在xml文件中多选标签Checkbox添加如下属性,此标签默认为选中;

isChecked()判断按钮是否处于被选中状态

setChecked(Boolean flag) 传递一个布尔参数来设置按钮的状态

CheckBox.getText();//获取多选框的值

Android复选框被选择时处理方法

setOnCheckedChangeListener()方法

Checkbox的直接父类是CompoundButton,间接父类是Button;

组件继承关系请参考:http://my.oschina.net/u/560730/blog/59133

定义布局文件checkbox.xml文件,内容如下

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout        android:orientation="vertical"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginRight="5dp" >          <TextView             android:id="@+id/checkbox_text_id"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:text="@string/Textview_content_web"            >           </TextView>          <CheckBox                android:id="@+id/checkbox_android_id"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:text="Android"              />          <CheckBox                android:id="@+id/checkbox_ios_id"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:text="IOS"              />          <CheckBox                android:id="@+id/checkbox_winphone_id"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:text="Windows Phone"              />       </LinearLayout> </ScrollView>

定义java文件

package com.dream.app.start.first.checkbox;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.dream.app.start.R;import com.dream.app.start.R.id;import com.dream.app.start.R.layout;import com.dream.app.start.R.string;import com.dream.app.start.utils.PublicClass;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.CheckBox;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.CompoundButton;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;import android.widget.ToggleButton;public class CheckBoxDemo extends Activity {  TextView   textview_button=null;private CheckBox  checkbox_Anroid,checkbox_Ios,checkboxWinphone;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.layout_first_checkbox);        //通过id获取id对应的组件checkbox_Anroid = (CheckBox)findViewById(R.id.checkbox_android_id);checkbox_Ios = (CheckBox)findViewById(R.id.checkbox_ios_id);checkboxWinphone = (CheckBox)findViewById(R.id.checkbox_winphone_id);//组件点击点击事件checkbox_Anroid.setOnClickListener(checklistener);checkbox_Ios.setOnClickListener(checklistener);checkboxWinphone.setOnClickListener(checklistener);}//点击事件private OnClickListener  checklistener = new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubTextView  txt = (TextView)v;switch (txt.getId()) {case R.id.checkbox_android_id:if(checkbox_Anroid.isChecked()){showToast("你选中的是:"+txt.getText().toString());}else{showToast("你取消的是:"+txt.getText().toString());}break;case R.id.checkbox_ios_id:if(checkbox_Ios.isChecked()){showToast("你选中的是:"+txt.getText().toString());}else{showToast("你取消的是:"+txt.getText().toString());}break;case R.id.checkbox_winphone_id:if(checkboxWinphone.isChecked()){showToast("你选中的是:"+txt.getText().toString());}else{showToast("你取消的是:"+txt.getText().toString());}break;default:break;}}};/** * toast * @param str */private void showToast(String str) {// TODO Auto-generated method stub         Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();}}

效果图:

[追加]:

1.继承自CompoundButton类的方法:
setChecked(boolean checked);//Changes the checked state of this button.

设置checkbox的check状态。 2.继承自View类的方法:

setSelected(boolean selected);//Changes the selection state of this view.

3.checked与selection两种状态的区别:

看看这段代码就明白了,无论是否checked,只要点击了这个checkbox,就被认为是selection。

import android.widget.CompoundButton.OnCheckedChangeListener;...CheckBox checkbox = (CheckBox)findViewById(R.id.checkbox);checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){@Overridepublic void onCheckedChanged(CompoundButton arg0, boolean arg1) {// TODO Auto-generated method stubString string = checkbox.isChecked() ? "Checked" : "Selected";checkbox.setText(string);}});



并不能改变checkbox的是否被check的状态。


更多相关文章

  1. 玩转Android---UI篇---ImageButton(带图标的按钮)
  2. Android(安卓)10适配注意的问题
  3. 一步步教你用Android(安卓)Google Map(一)
  4. Android(安卓)View架构总结
  5. Android点击Button实现功能的几种方法
  6. android 多用户管理UserManager
  7. 跟着官方学习Android(安卓)— Services
  8. Android(安卓)开发:第一日——明白Android(安卓)Activity生命周期
  9. 应聘Android开发工程师-Java笔试部分的答案及解析

随机推荐

  1. android 界面属性
  2. Android(安卓)DrawBitmap绘制图像
  3. android画一条虚线
  4. >>>> Android(安卓)adb shell后面可用的
  5. android声音服务
  6. Android通过PopupMenu定义弹出菜单的位置
  7. Android(安卓)UI Fundamentals Develop a
  8. android API Level 19 No system images
  9. Android(安卓)LOG机制流程图
  10. 关于android的webview打开淘宝天猫链接问