• 安卓开发_复选按钮控件(CheckBox)的简单使用


    复选按钮 即可以选择若干个选项,与单选按钮不同的是,复选按钮的图标是方块,单选按钮是圆圈

    复选按钮用CheckBox表示,CheckBox是Button的子类,支持使用Button的所有属性

    一、由于复选框可以选中多项,所有为了确定用户是否选择了某一项,还需要为每一个选项添加setOnCheckedChangeListener事件监听

    例如:

    为id为like1的复选按钮添加状态改变事件监听,代码如下

     1 final CheckBox like1 = (CheckBox)findViewById(R.id.like1);
     2     //监听事件
     3     
     4     like1.setOnCheckedChangeListener(new OnCheckedChangeListener()){
     5         
     6     @Override
     7     public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
     8         // TODO Auto-generated method stub
     9         if(like1.isChecked())
    10             like1.getText();
    11         }
    12     });

    二、使用示例

    先看布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="选择您的爱好"
            android:textSize="19dp"
            />
        <CheckBox 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/id_checkbox_1"
            android:text="音乐"
            />
        <CheckBox 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/id_checkbox_2"
            android:text="美术"
            />
        <CheckBox 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/id_checkbox_3"
            android:text="体育"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交"
            android:id="@+id/btn_checkbox_tijiao" 
            />
    </LinearLayout>

    效果图:

    再看JAVA文件

     1 package base_ui;
     2 
     3 import com.example.allcode.R;
     4 
     5 import android.app.Activity;
     6 import android.os.Bundle;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.widget.Button;
    10 import android.widget.CheckBox;
    11 import android.widget.Checkable;
    12 import android.widget.CompoundButton;
    13 import android.widget.RadioGroup.OnCheckedChangeListener;
    14 import android.widget.Toast;
    15 
    16 public class Ui_CheckBox extends Activity implements android.widget.CompoundButton.OnCheckedChangeListener{
    17     private Button tijiao;
    18     private CheckBox checkbox_1;
    19     private CheckBox checkbox_2;
    20     private CheckBox checkbox_3;
    21     private OnCheckedChangeListener checkbox_listen ;
    22     @Override
    23     protected void onCreate(Bundle savedInstanceState) {
    24         // TODO Auto-generated method stub
    25         super.onCreate(savedInstanceState);
    26         setContentView(R.layout.base_ui_checkbox);
    27     
    28         tijiao = (Button) findViewById(R.id.btn_checkbox_tijiao);
    29         
    30         checkbox_1 = (CheckBox) findViewById(R.id.id_checkbox_1);
    31         checkbox_2 = (CheckBox) findViewById(R.id.id_checkbox_2);
    32         checkbox_3 = (CheckBox) findViewById(R.id.id_checkbox_3);
    33         tijiao = (Button) findViewById(R.id.btn_checkbox_tijiao);
    34         
    35         checkbox_1.setOnCheckedChangeListener(this);
    36         checkbox_2.setOnCheckedChangeListener(this);
    37         checkbox_3.setOnCheckedChangeListener(this);
    38         
    39         tijiao.setOnClickListener(new OnClickListener() {
    40             
    41             @Override
    42             public void onClick(View v) {
    43                 // TODO Auto-generated method stub
    44                 String str="";   //存放选中的选项的值
    45                 if(checkbox_1.isChecked())
    46                     str+=checkbox_1.getText().toString()+" ";
    47                 if(checkbox_2.isChecked())
    48                     str+=checkbox_2.getText().toString()+" ";
    49                 if(checkbox_3.isChecked())
    50                     str+=checkbox_3.getText().toString()+" ";
    51                 Toast.makeText(Ui_CheckBox.this, "您选择的喜欢的爱好为:"+str, 1).show();
    52                 
    53                 
    54             }
    55         });
    56     }
    57     //监听事件
    58     @Override
    59     public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    60         // TODO Auto-generated method stub
    61         
    62     }
    63     
    64 }

    可以看到,代码是很简单的,只有一个方法需要学习

    checkbox_1.isChecked()

    返回checkbox_1对应的复选按钮控件是否被选中

    效果图:



  • 相关阅读:
    BZOJ 3330 分数
    FR #11题解
    BZOJ 1857 传送带
    BZOJ 4757 Building a Tall Barn
    FR #10题解
    BZOJ 4393 Fruit Feast
    BZOJ 3126 Photo
    BZOJ 1312 Hard Life
    BZOJ 2039 employ人员雇佣
    Count SIN Numbers
  • 原文地址:https://www.cnblogs.com/xqxacm/p/4510715.html
Copyright © 2020-2023  润新知