0

0

android 单选(RadioGroup )复选(CheckBox)按钮的应用

巴扎黑

巴扎黑

发布时间:2016-12-20 15:04:46

|

1566人浏览过

|

来源于php中文网

原创

第一步,还是创建android项目。

第二步,修改已经生成的res/layout/main.xml

 

view plaincopy to clipboardprint?

  

    android:orientation="vertical"  

    android:layout_width="fill_parent"  

    android:layout_height="fill_parent"  

>  

    

        android:id="@+id/checkBox"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="复选框1" />  

    

        android:id="@+id/checkBox1"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="复选框2" />  

    

        android:id="@+id/radioGroup"  

        android:orientation="vertical"  

        android:layout_width="fill_parent"  

        android:layout_height="fill_parent">  

        

            android:layout_width="wrap_content" android:layout_height="wrap_content"  

            android:text="北京" />  

        

            android:layout_width="wrap_content" android:layout_height="wrap_content"  

            android:text="天津" />  

        

            android:layout_width="wrap_content" android:layout_height="wrap_content"  

            android:text="上海" />  

        

            android:layout_width="wrap_content" android:layout_height="wrap_content"  

            android:text="重庆" />  

    

  

  

 

第三步,添加主要代码

 

view plaincopy to clipboardprint?

import android.app.Activity;  

import android.os.Bundle;  

import android.widget.CheckBox;  

import android.widget.CompoundButton;  

import android.widget.RadioButton;  

纯CSS实现的卡片切换效果
纯CSS实现的卡片切换效果

这是一款纯 CSS 实现的卡片切换效果,原理是通过单选按钮 radio 选择不同的按钮后,再使用 CSS 选择器选中对应的内容显示。

下载

import android.widget.RadioGroup;  

import android.widget.CompoundButton.OnCheckedChangeListener;  

  

  

public class checkBox extends Activity implements OnCheckedChangeListener  {  

    /** Called when the activity is first created. */  

      

      

    RadioButton  r1 = null;  

    RadioButton  r2 = null;  

    RadioButton  r3 = null;  

    RadioButton  r4 = null;  

      

    @Override  

    public void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.main);  

          

        CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);   

        CheckBox checkBox1 = (CheckBox) findViewById(R.id.checkBox1);  

        //获得单选按钮组  

        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);  

        //获得单选按钮  

        r1 = (RadioButton )findViewById(R.id.radion1);  

        r2 = (RadioButton )findViewById(R.id.radion2);  

        r3 = (RadioButton )findViewById(R.id.radion3);  

        r4 = (RadioButton )findViewById(R.id.radion4);  

          

        checkBox.setChecked(true);   

          

        r1.setClickable(true);  

        //监听多选按钮  

        checkBox.setOnCheckedChangeListener(this);  

        checkBox1.setOnCheckedChangeListener(this);  

        //监听单选按钮  

        radioGroup.setOnCheckedChangeListener(mChangeRadio);  

          

    }

相关专题

更多
C++ 高级模板编程与元编程
C++ 高级模板编程与元编程

本专题深入讲解 C++ 中的高级模板编程与元编程技术,涵盖模板特化、SFINAE、模板递归、类型萃取、编译时常量与计算、C++17 的折叠表达式与变长模板参数等。通过多个实际示例,帮助开发者掌握 如何利用 C++ 模板机制编写高效、可扩展的通用代码,并提升代码的灵活性与性能。

6

2026.01.23

php远程文件教程合集
php远程文件教程合集

本专题整合了php远程文件相关教程,阅读专题下面的文章了解更多详细内容。

21

2026.01.22

PHP后端开发相关内容汇总
PHP后端开发相关内容汇总

本专题整合了PHP后端开发相关内容,阅读专题下面的文章了解更多详细内容。

16

2026.01.22

php会话教程合集
php会话教程合集

本专题整合了php会话教程相关合集,阅读专题下面的文章了解更多详细内容。

11

2026.01.22

宝塔PHP8.4相关教程汇总
宝塔PHP8.4相关教程汇总

本专题整合了宝塔PHP8.4相关教程,阅读专题下面的文章了解更多详细内容。

9

2026.01.22

PHP特殊符号教程合集
PHP特殊符号教程合集

本专题整合了PHP特殊符号相关处理方法,阅读专题下面的文章了解更多详细内容。

9

2026.01.22

PHP探针相关教程合集
PHP探针相关教程合集

本专题整合了PHP探针相关教程,阅读专题下面的文章了解更多详细内容。

7

2026.01.22

菜鸟裹裹入口以及教程汇总
菜鸟裹裹入口以及教程汇总

本专题整合了菜鸟裹裹入口地址及教程分享,阅读专题下面的文章了解更多详细内容。

23

2026.01.22

Golang 性能分析与pprof调优实战
Golang 性能分析与pprof调优实战

本专题系统讲解 Golang 应用的性能分析与调优方法,重点覆盖 pprof 的使用方式,包括 CPU、内存、阻塞与 goroutine 分析,火焰图解读,常见性能瓶颈定位思路,以及在真实项目中进行针对性优化的实践技巧。通过案例讲解,帮助开发者掌握 用数据驱动的方式持续提升 Go 程序性能与稳定性。

9

2026.01.22

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 13万人学习

Java 教程
Java 教程

共578课时 | 49.7万人学习

Uniapp从零开始实现新闻资讯应用
Uniapp从零开始实现新闻资讯应用

共64课时 | 6.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号