CheckBox复选按钮是一种有双状态按钮的特殊类型,可以选中或者不选中。可以现在布局文件中定义多选按钮,然后对每一个多选按钮进行定义点击事件android:onclick 或者 事件监听setOnCheckedChangeListener,通过isChecked来判断选项是否被选中
1.界面
2.布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.liang.checkboxliang.MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_alignParentTop="true" android:text="Sports" android:textAppearance="?android:attr/textAppearanceLarge" /> <CheckBox android:id="@+id/cbsoccer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_below="@+id/textView" android:checked="false" android:onClick="cbclick" android:text="soccer" /> <CheckBox android:id="@+id/cbbasketball" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/cbsoccer" android:layout_toEndOf="@+id/cbsoccer" android:checked="false" android:onClick="cbclick" android:text="basketball" /> <CheckBox android:id="@+id/cbtennis" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/cbbasketball" android:layout_toEndOf="@+id/cbbasketball" android:checked="false" android:onClick="cbclick" android:text="tennis" /> </RelativeLayout>
3.代码
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private static List<String> list = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { list.clear(); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //也可以用监听方法 建议用android:onclick } public void cbclick(View view) { CheckBox cb = (CheckBox) view; boolean ischecked = cb.isChecked(); String content = cb.getText().toString(); switch (view.getId()) { case R.id.cbsoccer: //可以做点保存的工作 StringBuffer或者数据库等等 if (ischecked) { list.add(content); } else { list.remove(content); } if (list.size() > 0) { StringBuffer sb = new StringBuffer(""); for (String s : list) { sb.append(s + " "); } Toast.makeText(MainActivity.this, sb.toString(), Toast.LENGTH_SHORT).show(); } break; case R.id.cbbasketball: if (ischecked) { list.add(content); Toast.makeText(MainActivity.this, "basketball checked is true", Toast.LENGTH_SHORT).show(); } else { list.remove(content); Toast.makeText(MainActivity.this, "basketball checked is false", Toast.LENGTH_SHORT).show(); } break; case R.id.cbtennis: /* if (ischecked) { list.add(content); } else { list.remove(content); }*/ Toast.makeText(MainActivity.this, "tennis checked is " + ischecked, Toast.LENGTH_SHORT).show(); break; } } }