<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" />
</LinearLayout>
package com.demo2.demo2;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity{
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 buttonView, boolean isChecked) {
Log.i("tag", isChecked+"");
//通过这个onCheckedChanged方法来监听当前的checkBox是否被选中
if(isChecked){
//获得checkBox的文本内容
String text=checkBox.getText().toString();
Log.i("tag", text);//打印输出
}
}
});
}
}