欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android RadioButton和CheckBox组件的使用方法

程序员文章站 2022-07-11 19:32:23
radiobutton是单选按钮,多个radiobutton放在一个radiogroup控件中,也就是说每次只能有1个radiobutton被选中。而checkbox是多选...

radiobutton是单选按钮,多个radiobutton放在一个radiogroup控件中,也就是说每次只能有1个radiobutton被选中。而checkbox是多选按钮,toatst是android中带的一个用于显示提示小窗口消息的控件,其提示的内容过一会儿会自动消失。
radiogroup和checkbox控件设置监听器都是用的setoncheckedchangelistener函数,其输入参数是一个函数,且函数内部要实现1个内部类。radiogroup监听器的输入参数用的是radiogroup.oncheckedchangelistener(),而checkbox监听器的输入参数用的是函数compoundbutton.oncheckedchangelistener().
开发环境:android4.1

实验效果如下(采用的是线性布局):

效果图:
android RadioButton和CheckBox组件的使用方法


上面3个为一组radiogroup,每选中其中一个radiobutton,则会有相应的提示。且只能选中其中的一个。
下面的4都为checkbox,可以选中其中的多个。每个checkbox被选中或者取消选中都有相应的文字提示小窗口。

代码如下:
mainactivity.java:

复制代码 代码如下:

package com.example.control1;

import android.app.activity;
import android.os.bundle;
import android.widget.checkbox;
import android.widget.compoundbutton;
import android.widget.compoundbutton.oncheckedchangelistener;
import android.widget.radiobutton;
import android.widget.radiogroup;
import android.widget.textview;
import android.widget.toast;

public class mainactivity extends activity {

    //定义各控件的变量
    private textview who = null;
    private textview how = null;
    private radiogroup who_group = null;
    private radiobutton china = null;
    private radiobutton america = null;
    private radiobutton others = null;
    private checkbox less = null;
    private checkbox thirty = null;
    private checkbox forty = null;
    private checkbox fifty = null;
    @override
    public void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);

        //获得对应的控件
        who = (textview)findviewbyid(r.id.who);
        how = (textview)findviewbyid(r.id.how);
        who_group = (radiogroup)findviewbyid(r.id.who_group);
        china = (radiobutton)findviewbyid(r.id.china);
        america = (radiobutton)findviewbyid(r.id.america);
        others = (radiobutton)findviewbyid(r.id.others);
        less = (checkbox)findviewbyid(r.id.less);
        thirty = (checkbox)findviewbyid(r.id.thirty);
        forty = (checkbox)findviewbyid(r.id.forty);
        fifty = (checkbox)findviewbyid(r.id.fifty);

        //设置who_group的监听器,其实是一句代码,其参数是一个带有重构函数的对象
        who_group.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() {

   public void oncheckedchanged(radiogroup group, int checkedid) {
       // todo auto-generated method stub
       if(checkedid == china.getid()){
  toast.maketext(mainactivity.this,"中国", toast.length_short).show();
       }
       else if(checkedid == america.getid()){
  toast.maketext(mainactivity.this, "美国", toast.length_short).show();
       }
       else if(checkedid == others.getid()){
  toast.maketext(mainactivity.this, "其它国家", toast.length_short).show();
       }
   }
        });

        //下面为4个checkbox多选按钮分别建立监听器
        less.setoncheckedchangelistener(new oncheckedchangelistener() {

   public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
       // todo auto-generated method stub
       if(ischecked)
       {
  toast.maketext(mainactivity.this, "30个以下", toast.length_short).show();
       }
       else{
  toast.maketext(mainactivity.this, "不是30个以下", toast.length_short).show();
       }
   }
        });

       
      //下面为4个checkbox多选按钮分别建立监听器
        thirty.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {

   public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
       // todo auto-generated method stub
       if(ischecked)
       {
  toast.maketext(mainactivity.this, "30~39", toast.length_short).show();
       }
       else{
  toast.maketext(mainactivity.this, "不是30~39", toast.length_short).show();
       }
   }
        });

      //下面为4个checkbox多选按钮分别建立监听器
        forty.setoncheckedchangelistener(new oncheckedchangelistener() {

   public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
       // todo auto-generated method stub
       if(ischecked)
       {
  toast.maketext(mainactivity.this, "40~49", toast.length_short).show();
       }
       else{
  toast.maketext(mainactivity.this, "不是40~49", toast.length_short).show();
       }
   }
        });

      //下面为4个checkbox多选按钮分别建立监听器
        fifty.setoncheckedchangelistener(new oncheckedchangelistener() {

   public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {
       // todo auto-generated method stub
       if(ischecked)
       {
  toast.maketext(mainactivity.this, "50以上", toast.length_short).show();
       }
       else{
  toast.maketext(mainactivity.this, "不是50以上", toast.length_short).show();
       }
   }
        });

   

    }
}



activity_main.xml:
复制代码 代码如下:

<linearlayout 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:orientation="vertical"
    >
    <textview
        android:id="@+id/who"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/who"
/>
    <radiogroup
        android:id="@+id/who_group"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical"
      >
       <radiobutton
  android:id="@+id/china"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
  android:text="@string/china"
  />
       <radiobutton
  android:id="@+id/america"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/america"
  />
       <radiobutton
  android:id="@+id/others"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/others"
  />
       </radiogroup>
       <textview
  android:id="@+id/how"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/how"
  />
        <checkbox
  android:id="@+id/less"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/less"
  />
       <checkbox
  android:id="@+id/thirty"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/thirty"
  />
       <checkbox
  android:id="@+id/forty"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/forty"
  />
       <checkbox
  android:id="@+id/fifty"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/fifty"
  />

</linearlayout>



实验总结:通过本次实验对radiogroup,checkbox,radiobutton和toast这4个控件的简单使用有了个初步的了解。

作者:tornadomeet