`
1450901761
  • 浏览: 7899 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
文章分类
社区版块
存档分类
最新评论

基本组件之RadioGroup与CheckBox

阅读更多
1.单选按钮使用RadioButton组件,通常与RadioGroup一起使用
RadioButton是Button的子类,所以RadioButton可以使用Button的支持的各种属性
RadioButton组件中的android:checked 属性用于指定选中状态,属性值为true,表示选中;属性值为false时,表示取消选中,默认为false。

<RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/radiogroup1">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="20px"
            android:id="@+id/radio1"
            android:checked="true"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="20px"
            android:id="@+id/radio2"/>
    </RadioGroup>

在虚拟机上运行:


2.获取单选按钮组中选中项的值
获取选中项的值有俩种方法:一种是改变按钮组的值时获取,另一种是单击其他按钮时获取
(1)改变按钮组的值时获取
首先获取单选按钮组,然后添加OnCheckedChangeListener,获取被选中的单选按钮,通过getText()方法获取单选按钮对应的值。

final RadioGroup sex = (RadioGroup)findViewById(R.id.radiogroup1); 
sex.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 
    @Override 
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
    RadioButton r = (RadioButton)findViewById(checkedId); 
    r.getText();
    }        
});
(2)单击其他按钮时获取
首先需要在该按钮单击事件监听器的onClick()方法中,通过for循环语句遍历当前单选按钮组,并根据被遍历到的单选按钮组的isChecked()方法判断该按钮是否被选中,当被选中时,通过单选按钮的getText()方法获取对应的值。

    final RadioGroup sex = (RadioGroup)findViewById(R.id.radoiGroup);  
    submitBtn.setOnClickListener(new OnClickListener(){ 
     
        @Override 
        public void onClick(View arg0) { 
            // TODO Auto-generated method stub 
            for(int i=0; i<sex.getChildCount(); i++){ 
                RadioButton r = (RadioButton)sex.getChildAt(i); 
                if(r.isChecked()){ 
                    r.getText(); 
                    break; 
                } 
            } 
        } 
    }); 
3.给提交按钮设置按钮单击事件监听

Button button = (Button)findViewById(R.id.radiobutton1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
for (int i = 0; i <sex.getChildCount();) {
RadioButton r = (RadioButton) sex.getChildAt(i);
Log.i("单选按钮", "性别:"+r.getText());
break;
}
}
});

4.复选框使用的是checkbox组件

<CheckBox
        android:text="体育"
        android:id="@+id/like1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:text="美术"
        android:id="@+id/like2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:text="音乐"
        android:id="@+id/like3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

5.创建并实例化一个OnCheckedChangeListener对象,获取3个复选框并为其设置就监听器

final CheckBox like1=(CheckBox)findViewById(R.id.like1);
        final CheckBox like2=(CheckBox)findViewById(R.id.like2);
        final CheckBox like3=(CheckBox)findViewById(R.id.like3);
        like1.setOnCheckedChangeListener(checkBox_listener);
        like2.setOnCheckedChangeListener(checkBox_listener);
        like3.setOnCheckedChangeListener(checkBox_listener);

6.为提交按钮添加监听器

Button button = (Button) findViewById(R.id.checkbutton1);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String like="";
if(like1.isChecked())
like+=like1.getText().toString()+" ";
if(like2.isChecked())
like+=like2.getText().toString()+" ";
if(like3.isChecked())
like+=like3.getText().toString()+" ";
Toast.makeText(CheckActivity.this, like, Toast.LENGTH_SHORT).show();


  • 大小: 48.5 KB
  • 大小: 29.8 KB
分享到:
评论

相关推荐

    界面控件之选项组件(RadioGroup和CheckBox)--代码

    界面控件之选项组件(RadioGroup和CheckBox)--代码,Android开发学习26,博客主页: http://blog.csdn.net/ypist.

    android RadioButton和CheckBox组件的使用方法

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

    Android 超多基础组件使用范例技巧大全.rar

    这些组件大多与日常的Android编程息息相关,且更多 的时候是一种UI界面的必备元素,这些必备元素控件包括了以下内容:AutoCompleteTextView、CheckBox、CopyOfTimePicker、DatePicker、EditText、GridView、...

    react-ui-icheck:使用React构建的iCheck可自定义复选框和无线电

    差异性您可以在下面找到与原始库的主要区别的列表: 重构并改进了现有的代码库: 添加了onBlur和onFocus标签事件,以伴随onMouseOut和onMouseOver 将所有相应的属性从EnchantedSwitch移至Checkbox和Radio组件,并...

    集成目前Android主流优秀第三方组件

    追加自定义属性Value的CheckBox/RadioButton/RadioGroup/SingleSpinner 圆角提示信息TipsView 圆角图片RoundImageView 自定义样式风格ProgressDialog 自定义样式WebView 圆形进度条(RoundProgressBar)、垂直进度条...

    安卓智能终端界面

    用于安卓手机界面的单选复选按钮的代码,简单熟练掌握RadioGroup RadioButton、CheckBox组件的用法功能

    Delphi7.完美经典

    10-14 RadioGroup组件 10-15 Panel组件 10-16 ActionList组件 第11章 TApplication与TScreen类介绍及应用 11-1 TApplication类 11-1-1 TApplication类对象常用的属性 11-1-2 TApplication类对象常用的方法 ...

    Delphi7.完美经典.part1

    10-8 CheckBox组件 10-9 RadioButton组件 10-10 ListBox组件 10-11 ComboBox组件 10-12 ScrollBar组件 10-13 GroupBox组件 10-14 RadioGroup组件 10-15 Panel组件 10-16 ...

    Android基础学习代码

    包含: Button CheckBox RadioGroup等基础组件用法 文章地址: http://hi.baidu.com/yanglinjingshu

    集成安卓主流优秀第三方组件框架.zip

    追加自定义属性Value的CheckBox/RadioButton/RadioGroup/SingleSpinner 圆角提示信息TipsView 圆角图片RoundImageView 自定义样式风格ProgressDialog 自定义样式WebView 圆形进度条(RoundProgressBar)、垂直...

    收集一些Android widget窗口小部件用法实例集.rar

    为大家分享一套Android widget窗口小部件用法实例集源码,在这个例子中,将涉及到一些大家非常熟悉而且非常有用的窗体UI组件的用法,比如Button按钮、textView、Editview编辑框、Checkbox选择框、RadioGroup单选框、...

    Android集成主流优秀第三方组件框架

    这是一个集成目前Android主流优秀第三方组件、优秀好用的自定义控件、...追加自定义属性Value的CheckBox/RadioButton/RadioGroup/SingleSpinner 圆角提示信息TipsView 圆角图片RoundImageView 自定义样式风格Progres

    android常用控件综合应用

    Android常用控件的声明 TextView:文本显示框 EditView:文本编辑框 Button:按钮 Menu:菜单 RadioButton:单选按钮 ... RadioGroup:单选按钮组 CheckBox:复选框 ScrollView:滚动条

    黑马程序员 安卓学院 万元哥项目经理 分享220个代码实例

    |--监听之CheckBox是否选中监听 |--监听之EditText内容变化监听 |--监听之GridView条目点击监听 |--监听之ListView条目点击事件监听 |--监听之ListView滑动监听 |--监听之单击监听的两种定义 |--监听之双击监听 |--...

    Android例子源码集成安卓主流优秀第三方组件框架.zip

    追加自定义属性Value的CheckBox/RadioButton/RadioGroup/SingleSpinner 圆角提示信息TipsView 圆角图片RoundImageView 自定义样式风格ProgressDialog 自定义样式WebView 圆形进度条(RoundProgressBar)、垂直...

    Android studio APP开发 单选框和复选框

    通常情况下,RadioButton组件需要与RadioGroup组件一起使用。 设置单选框 android:checked 为指定选中状态,即设定一个默认选择的按钮。 获取单选框组中选中项的值 通常在以下两种情况下获取单选框组中选中项的值...

    实验3-Android-应用的界面开发(2).doc

    实验目的 1、掌握常用界面组件TextView、EditText、Button、ImageButton、RadioGroup CheckBox、ImageView、AutoCompleteTextView、Spinner 2、掌握Toast的用法 2. 实验步骤 1. 实现"radionbutton"的功能 参考代码...

    android开发入门与实战(下)

    7.3.6 单项选择(RadioGroup)介绍与应用 7.3.7 下拉列表(Spinner)介绍与应用 7.3.8 自动完成文本(AutoCompleteTextView) 7.3.9 日期选择器(DatePicker)介绍与应用 7.3.10 时间选择器(TimePicker)介绍与应用 7.3.11 ...

Global site tag (gtag.js) - Google Analytics