使用CheckBox实现多选效果

来源:互联网 发布:拜占庭将军问题 算法 编辑:程序博客网 时间:2024/06/08 20:37
<?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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//初始化checkBoxcheckBox=(CheckBox)findViewById(R.id.checkBox1);//通过设置checkBox的监听事件来对checkBox是不是被选中checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {Log.i("tag", isChecked+"");//通过这个onCheckedChanged方法来监听当前的checkBox是否被选中if(isChecked){//获得checkBox的文本内容String text=checkBox.getText().toString();Log.i("tag", text);//打印输出}}});}}

0 0
原创粉丝点击