Android组件之CheckBox

本文详细介绍了一个Android应用中复选框(Checkbox)的使用方法,包括如何设置默认选项、注册事件监听器以及获取用户的选择结果。通过示例代码展示了不同方式设置监听器的过程,并解释了如何将用户的选择结果展示给用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.example.day16_checkbox;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class MainActivity extends Activity {

 private CheckBox cbx1, cbx2, cbx3, cbx4;

 private List<CheckBox> cbxs = new ArrayList<CheckBox>();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  cbx1 = (CheckBox) findViewById(R.id.checkBox1);
  cbx2 = (CheckBox) findViewById(R.id.checkBox2);
  cbx3 = (CheckBox) findViewById(R.id.checkBox3);
  cbx4 = (CheckBox) findViewById(R.id.checkBox4);
  // 默认选项
  cbx1.setChecked(true);
  cbx3.setChecked(true);
  // 注册事件
  /*
   * 第一种 cbx1.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
   * cbx2.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
   * cbx3.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
   * cbx4.setOnCheckedChangeListener(new MyOnCheckedChangeListener());
   */
  // 第二种
  cbx1.setOnCheckedChangeListener(listener);
  cbx2.setOnCheckedChangeListener(listener);
  cbx3.setOnCheckedChangeListener(listener);
  cbx4.setOnCheckedChangeListener(listener);

  // 添加到集合中
  cbxs.add(cbx1);
  cbxs.add(cbx2);
  cbxs.add(cbx3);
  cbxs.add(cbx4);

  cbx1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {
    // TODO Auto-generated method stub
   }
  });
 }

 public void getValues(View v) {
  String content = "";

  for (CheckBox cbx : cbxs) {
   if (cbx.isChecked()) {
    content += cbx.getText() + "\n";
   }
  }

  if ("".equals(content)) {
   content = "您还没有选择";
  }
  new AlertDialog.Builder(this).setMessage(content).setTitle("选中内容如下")
    .setPositiveButton("关闭", null).show();

 }

 // 第一种
 /*
  * class MyOnCheckedChangeListener implements
  * CompoundButton.OnCheckedChangeListener {
  *
  * @Override public void onCheckedChanged(CompoundButton buttonView, boolean
  * isChecked) { // TODO Auto-generated method stub CheckBox box = (CheckBox)
  * buttonView;
  *
  * Toast.makeText(getApplicationContext(), "获取的值:" + isChecked + "xxxxx" +
  * box.getText(), Toast.LENGTH_LONG).show();
  *
  * } }
  */
 // 第二种
 CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener() {

  @Override
  public void onCheckedChanged(CompoundButton buttonView,
    boolean isChecked) {
   // TODO Auto-generated method stub

   CheckBox box = (CheckBox) buttonView;

   Toast.makeText(getApplicationContext(),
     "获取的值:" + isChecked + "xxxxx" + box.getText(),
     Toast.LENGTH_LONG).show();

  }
 };

}
==================================================================================================

<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=".MainActivity" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="41dp"
        android:text="@string/text_java" />

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox2"
        android:layout_centerVertical="true"
        android:text="@string/text_php" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/checkBox1"
        android:layout_below="@+id/checkBox1"
        android:layout_marginTop="22dp"
        android:text="@string/text_3g" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/checkBox4"
        android:layout_alignLeft="@+id/checkBox4"
        android:layout_marginBottom="17dp"
        android:text="@string/text_net" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/checkBox4"
        android:layout_below="@+id/checkBox4"
        android:onClick="getValues"
        android:layout_marginTop="34dp"
        android:text="@string/text_get" />

</RelativeLayout>

=================================================================================================

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">day16-checkbox</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="text_java">Java专业</string>
    <string name="text_3g">3G专业</string>
    <string name="text_net">.Net专业</string>
    <string name="text_php">PHP专业</string>
    <string name="text_get">查询</string>

</resources>

================================================================================================

效果图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值