博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android开发历程_6(RadioButton和CheckBox的使用)
阅读量:6977 次
发布时间:2019-06-27

本文共 5470 字,大约阅读时间需要 18 分钟。

         本次实验中主要是学习如何使用RadioGroup,CheckBox,RadioButton和Toast这几个控件,其中RadioButton是单选按钮,多个RadioButton放在一个RadioGroup控件中,也就是说每次只能有1个RadioButton被选中。而CheckBox是多选按钮,Toatst是android中带的一个用于显示提示小窗口消息的控件,其提示的内容过一会儿会自动消失。

         RadioGroup和CheckBox控件设置监听器都是用的setOnCheckedChangeListener函数,其输入参数是一个函数,且函数内部要实现1个内部类。RadioGroup监听器的输入参数用的是RadioGroup.OnCheckedChangeListener(),而CheckBox监听器的输入参数用的是函数CompoundButton.OnCheckedChangeListener().

   开发环境:android4.1

 

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

      效果图1:

    

   效果图2:

    

 

         上面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:

 

 

  实验总结:通过本次实验对RadioGroup,CheckBox,RadioButton和Toast这4个控件的简单使用有了个初步的了解。

 

 

 

 

 

 

转载于:https://www.cnblogs.com/tornadomeet/archive/2012/07/30/2614510.html

你可能感兴趣的文章
杨学海:跨境电商新通道-进口保税直邮模式解析
查看>>
rsync client backup scripts
查看>>
L09-10老男孩Linux运维实战培训-Nginx服务生产实战应用指南05(架构解决方案)
查看>>
Linux下的Shell工作原理
查看>>
FTP与TFTP的区别
查看>>
Zookeeper迁移(扩容/缩容)
查看>>
jQuery中的Ajax----03
查看>>
思科生成树命令之debug spanning-tree(本文转载自:www.91ccie.coml
查看>>
精品软件 推荐 瑞星 杀毒软件 安全软件
查看>>
精品软件 推荐 硬盘物理序列号修改专家
查看>>
tomcat简单配置
查看>>
Ansible基础一Playbook(二)
查看>>
MySQL5.6.16二进制源码安装详解及一键安装实现
查看>>
好久没有更新了!
查看>>
Netscaler 认证,访问报http 5000 内部错误
查看>>
Tomcat:Connection reset by peer: socket write error
查看>>
ARP(Accounting Resource Planning)项目感想
查看>>
Linux系统基础-管理之用户、权限管理
查看>>
Nginx(二) 配置与调试
查看>>
A first look at Xync Lync client on iOS iPhone/iPad
查看>>