效果图:
activity_checkbox.xml布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".CheckboxActivity" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tv_hobby" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的爱好:" android:textSize="20sp" /> <CheckBox android:id="@+id/ck_book" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="读书" /> <CheckBox android:id="@+id/ck_sport" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="运动" /> <CheckBox android:id="@+id/ck_travel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="旅游" /> </LinearLayout> <Button android:id="@+id/btn_commit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="提交" /> </LinearLayout>
CheckboxActivity代码:
public class CheckboxActivity extends AppCompatActivity implements View.OnClickListener { private CheckBox ck_book,ck_sport,ck_travel; private Button btn_commit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_checkbox); ck_book =findViewById(R.id.ck_book); ck_sport =findViewById(R.id.ck_sport); ck_travel =findViewById(R.id.ck_travel); btn_commit = findViewById(R.id.btn_commit); btn_commit.setOnClickListener(this); //给复选框添加选中状态改变的监听器 ck_book.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //如果选中则获取选中的值 if (ck_book.isChecked()){ Toast.makeText(CheckboxActivity.this, "我的爱好:"+ck_book.getText(), Toast.LENGTH_SHORT).show(); } } }); ck_sport.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (ck_sport.isChecked()){ Toast.makeText(CheckboxActivity.this, "我的爱好:"+ck_sport.getText(), Toast.LENGTH_SHORT).show(); } } }); ck_travel.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (ck_travel.isChecked()){ Toast.makeText(CheckboxActivity.this, "我的爱好:"+ck_travel.getText(), Toast.LENGTH_SHORT).show(); } } }); } //获取复选框选中的值 @Override public void onClick(View v) { //定义一个可变字符序列 用来存储复选框选中的值 StringBuilder builder = new StringBuilder(); if (ck_book.isChecked()) { builder.append(ck_book.getText()+","); } if (ck_sport.isChecked()) { builder.append(ck_sport.getText()+","); } if (ck_travel.isChecked()) { builder.append(ck_travel.getText()+","); } Toast.makeText(this, "我的爱好为:"+builder, Toast.LENGTH_LONG).show(); } }