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

巴扎黑
发布: 2016-12-20 15:04:46
原创
1515人浏览过

第一步,还是创建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;  

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);  

          

    }

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

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