iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >android RadioButton和CheckBox组件的使用方法
  • 471
分享到

android RadioButton和CheckBox组件的使用方法

radiobuttoncheckbox方法Android 2022-06-06 10:06:35 471人浏览 安东尼
摘要

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
实验效果如下(采用的是线性布局):
效果图:


上面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() {
   publi c 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

您可能感兴趣的文章:Android开发仿iOS滑动开关实现代码Android开发进阶自定义控件之滑动开关实现方法【附demo源码下载】Android自定义控件实现滑动开关效果Android开发之WEBView组件的使用解析android开发教程之view组件添加边框示例Android SearchView搜索框组件的使用方法android WebView组件使用总结Android四大组件之Service(服务)实例详解Android组件实现列表选择框功能Android自定义加载loading view动画组件Android编程实现滑动开关组件功能【附源码下载】


--结束END--

本文标题: android RadioButton和CheckBox组件的使用方法

本文链接: https://www.lsjlt.com/news/27294.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android控件RadioButton的使用方法
    本文实例为大家分享了Android控件RadioButton的使用代码,供大家参考,具体内容如下 内容 <?xml version="1.0" encoding=...
    99+
    2024-04-02
  • Android单选按钮RadioButton的使用方法
    单选按钮要在一组中选择一项,并且不能多选。 同一组RadioButton要放在同一个RadioGroup节点下。 RadioButton默认未选中,点击后选中但是再次点击不会取消选中...
    99+
    2024-04-02
  • android radiobutton控件如何使用
    要使用Android RadioButton控件,您需要遵循以下步骤:1. 在XML布局文件中添加RadioButton控件。例如,...
    99+
    2023-09-21
    android
  • Android中CheckBox复选框控件使用方法详解
    CheckBox复选框控件使用方法,具体内容如下一、简介类结构图二、CheckBox复选框控件使用方法这里是使用java代码在LinearLayout里面添加控件新建LinearLayout布局建立CheckBox的XML的Layout文件...
    99+
    2023-05-30
    checkbox 复选框 roi
  • Flutter多选按钮组件Checkbox使用方法详解
    Flutter 中的多选按钮组件有两种,供大家参考,具体内容如下 1. Checkbox 多选按钮,一般用来表现一些简单的信息。 常用属性如下: (1). value  多...
    99+
    2024-04-02
  • Android单选按钮RadioButton的使用详解
    RadioButton是一种用于在Android应用程序中提供单选选项的视图组件。它通常与RadioGroup组件一起使用,以便只能...
    99+
    2023-08-15
    Android
  • Android复选框CheckBox与开关按钮Switch及单选按钮RadioButton使用示例详解
    目录前言 一、复选框CheckBox二、开关按钮Switch三、单选按钮RadioButton单选组的用法前言  CompoundButton在XML文件中主要...
    99+
    2024-04-02
  • Android开发设置RadioButton点击效果的方法
    本文实例讲述了Android开发设置RadioButton点击效果的方法。分享给大家供大家参考,具体如下:在安卓开发中用到底部菜单栏 需要用到RadioButton这个组件实际应用的过程中,需要对按钮进行点击,为了让用户知道是否点击可这个按...
    99+
    2023-05-31
    android radiobutton roi
  • Android Spinner和GridView组件的使用示例
    目录一. 概述:二. 实现三. 美化四. GridView一. 概述:         Spinner是我们所熟悉的下拉框列表。与ListVi...
    99+
    2024-04-02
  • c# WPF中CheckBox样式的使用方法
    本篇内容主要讲解“c# WPF中CheckBox样式的使用方法 ”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“c# WPF中CheckBox样式的使用方法 ”吧!背景  很多时候我们使用WPF开...
    99+
    2023-06-07
  • Vue之组件的使用方法
    这篇文章主要介绍了Vue之组件的使用方法,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。组件是什么官方的定义:组件是可复用的 Vue 实例,并...
    99+
    2024-04-02
  • Android组件化工具ARouter使用方法详细分析
    目录前言原理简述基本使用前言 组件,就是对数据和方法的简单封装,功能单一,高类聚,是业务划分的最小粒度。组件化是基于可重用的目的,将大型软件系统按照分离关注点的形式,拆分成多个独立组...
    99+
    2022-11-13
    Android组件化工具ARouter Android组件化 Android ARouter
  • vue中keepAlive组件的作用和使用方法详解
    前言 在面试的时候,很多面试官再问vue的时候可能就会提一嘴,你知道keep-alive有什么作用吗? keep-alive是vue内置的一个组件,而这个组件的作用就是能够缓存不活动...
    99+
    2024-04-02
  • android轮播图组件的制作方法
    本文实例为大家分享了android轮播图组件的制作方法,供大家参考,具体内容如下 BannerLayout package com.coral3.common_module.co...
    99+
    2024-04-02
  • Android的BottomSheetDialog组件如何使用
    这篇“Android的BottomSheetDialog组件如何使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Andro...
    99+
    2023-07-02
  • Android的AdapterView组件怎么使用
    本篇内容介绍了“Android的AdapterView组件怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!概述  ...
    99+
    2023-06-29
  • React中使用antd组件的方法
    目录antd使用antdantd antd 是基于 Ant Design 设计体系的 React UI 组件库,主要用于研发企业级中后台产品。 国内镜像为:https://ant-d...
    99+
    2024-04-02
  • Android Jetpack组件Navigation导航组件的基本使用
    目录1.Navigation 基本概念2.Navigation 使用入门2.1 添加Navigation依赖2.2 创建导航图2.3 导航图中添加目的地Fragment2.4 Act...
    99+
    2024-04-02
  • input标签checkbox选中触发事件的方法
    您可以使用JavaScript来监听checkbox的选中状态,并在选中时触发相应的事件。下面是一个示例代码:HTML代码:```h...
    99+
    2023-10-12
    checkbox
  • aspjpeg组件使用方法284415实现方法
    1、什么是AspJpeg? AspJpeg是一款功能强大的基于Microsoft IIS环境的图片处理组件,网络上对其进行详细和深入介绍的中文文章并不多,即使有一般也只是牵涉到图片缩...
    99+
    2023-05-20
    aspjpeg组件使用方法
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作