当前位置: 首页 > news >正文

Android控件-单选按钮RadioButton

RadioGroup单选按钮用法,还是先看效果图

先中后,点RadioGroup测试按钮,可在标题栏显示选择结果,点清除可以清除选择。
下面上代码,main.xml:

<RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:checkedButton="@+id/b1"
    android:id="@+id/RG">
<!--默认选中b1-->
    <RadioButton 
        android:text="1"
        android:id="@+id/b1"
    />
     <RadioButton 
        android:text="2"
        android:id="@+id/b2"
    />
     <RadioButton 
        android:text="3"
        android:id="@+id/b3"
    />
</RadioGroup>
<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/show"
    android:text="RadioGroup测试"
/>
<Button 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/clear"
    android:text="清除"
/>

 程序代码:

package com.pocketdigi;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
 
public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("RadioGroup测试");
        setContentView(R.layout.main);
        RGDemo();
    }
    RadioGroup rg;
    RadioButton b1;
    RadioButton b2;
    RadioButton b3;
    public void RGDemo(){
        rg=(RadioGroup)findViewById(R.id.RG);
        b1=(RadioButton)findViewById(R.id.b1);
        b2=(RadioButton)findViewById(R.id.b2);
        b3=(RadioButton)findViewById(R.id.b3);
        Button clr=(Button)findViewById(R.id.clear);
        clr.setOnClickListener(clear);
        Button echo=(Button)findViewById(R.id.show);
        echo.setOnClickListener(show);
    }
    private Button.OnClickListener clear=new OnClickListener(){
 
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            rg.clearCheck();
            setTitle("RadioGroup测试");
        }
 
    };
    private OnClickListener show=new OnClickListener(){
 
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(b1.isChecked()){
                setTitle("1");
            }
            if(b2.isChecked()){
                setTitle("2");
            }
            if(b3.isChecked()){
                setTitle("3");
            }
        }
 
    };
}

 RadioGroup有一个onCheckChangeListener监听器,可以通过监听器的onCheckedChanged方法捕捉到点击事件,onCheckedChanged方法会传入一个int型的checkedId,可以通过对比传入的checkedId和RadioButton的ID,来确定被点中的选项.

rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){
 
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                if(checkedId==b1.getId()){
                    Toast.makeText(main.this,"b1选中", Toast.LENGTH_LONG).show();
                }
                if(checkedId==b2.getId()){
                    Toast.makeText(main.this,"b2选中", Toast.LENGTH_LONG).show();
                }
                if(checkedId==b3.getId()){
                    Toast.makeText(main.this,"b3选中", Toast.LENGTH_LONG).show();
                }
 
 
            }
 
        });

 

转载于:https://www.cnblogs.com/cuishuang/p/5729942.html

相关文章:

  • 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化
  • Linux上分析java程序的问题
  • OC点语法和变量作用域
  • Docker个人学习总结
  • Java NIO 系列教程 转
  • git常用命令以及速查命令
  • 数字电路基础(网络整理)
  • Vue.js学习笔记(4)
  • mysql数据库权限及编码
  • web前端之HTML的前世今生
  • Could not find or load main class org.gradle.wrapper.GradleWrapperMain解决办法
  • HTML5 图片边框
  • 关于单片机P3口的值对单片机串口通信的影响
  • python day14
  • WebAPI
  • 实现windows 窗体的自己画,网上摘抄的,学习了
  • ➹使用webpack配置多页面应用(MPA)
  • CSS相对定位
  • Docker 1.12实践:Docker Service、Stack与分布式应用捆绑包
  • iOS小技巧之UIImagePickerController实现头像选择
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • PAT A1017 优先队列
  • Python爬虫--- 1.3 BS4库的解析器
  • spring学习第二天
  • Vue2.x学习三:事件处理生命周期钩子
  • 从tcpdump抓包看TCP/IP协议
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 从重复到重用
  • 解析 Webpack中import、require、按需加载的执行过程
  • 前端js -- this指向总结。
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 算法-插入排序
  • 中文输入法与React文本输入框的问题与解决方案
  • 交换综合实验一
  • 专访Pony.ai 楼天城:自动驾驶已经走过了“从0到1”,“规模”是行业的分水岭| 自动驾驶这十年 ...
  • # 再次尝试 连接失败_无线WiFi无法连接到网络怎么办【解决方法】
  • ${factoryList }后面有空格不影响
  • (02)Hive SQL编译成MapReduce任务的过程
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (二)七种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (三)终结任务
  • (十)c52学习之旅-定时器实验
  • (学习日记)2024.02.29:UCOSIII第二节
  • (自用)learnOpenGL学习总结-高级OpenGL-抗锯齿
  • .describe() python_Python-Win32com-Excel
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现
  • .NET 设计一套高性能的弱事件机制
  • .net 桌面开发 运行一阵子就自动关闭_聊城旋转门家用价格大约是多少,全自动旋转门,期待合作...
  • @Conditional注解详解
  • @DependsOn:解析 Spring 中的依赖关系之艺术
  • @value 静态变量_Python彻底搞懂:变量、对象、赋值、引用、拷贝
  • [2018-01-08] Python强化周的第一天
  • [ai笔记4] 将AI工具场景化,应用于生活和工作