<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择您喜欢的科目"
></TextView>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="语文" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数学" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="英语" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="物理" />
</LinearLayout>
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends Activity {
private CheckBox ck1,ck2,ck3;//初始化CheckBox
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.ck1=(CheckBox)super.findViewById(R.id.checkBox1);//获得ck1
this.ck3=(CheckBox)super.findViewById(R.id.checkBox3);//获得ck2
this.ck2=(CheckBox)super.findViewById(R.id.checkBox4);//获得ck3
ck1.setChecked(true);//设置选中
ck2.setChecked(true);//设置选中
ck3.setChecked(true);//设置选中
//默认选中1、3、4项
}
}
从零开始学android<CheckBox多选按钮的使用.八.>,布布扣,bubuko.com
从零开始学android<CheckBox多选按钮的使用.八.>
原文:http://blog.csdn.net/u013616976/article/details/23127057