RadioGroup,Radio单选按钮,CheckBox的使用

xml文件中:

<RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="45dp" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="pepelu" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="cc" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="lulu" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup1"
        android:layout_below="@+id/radioGroup1"
        android:layout_marginLeft="83dp"
        android:layout_marginTop="147dp"
        android:text="TextView" />

activity中:

public class MainActivity extends Activity {
    TextView textView;
    HashMap<Integer, String> radio;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        textView = (TextView) findViewById(R.id.textView1);
        radio=new HashMap<Integer, String>();
        radio.put(R.id.radio0, "cc");
        radio.put(R.id.radio1, "pepelu");
        radio.put(R.id.radio2, "lulu");
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                textView.setText(radio.get(checkedId));
            }
        });
    }

}

使用HashMap保存数据是因为Radio的id不是连续的,并且是唯一的,非常符合HashMap的key规则。

使用OnCheckedChangListener监听RadioGroup中单个Radio的选中状态。

TextView输出对应结果。

CheckBox使用:

 public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.content_layout_id);

         final CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox_id);
         if (checkBox.isChecked()) {
             checkBox.setChecked(false);
         }
     }
 }
时间: 2024-07-29 08:16:13

RadioGroup,Radio单选按钮,CheckBox的使用的相关文章

Jquery常用操作:checkbox、select取值,radio、checkbox、select选中及其相关

常用Jquery操作:checkbox取值.select取值.radio选中.checkbox选中.select选中及其相关: 1.影藏页面元素 使用jquery真的很方便,比如要控制div的显示与隐藏,一句话就搞定了,请看下面使用说明. $("#id").show()表示display:block, $("#id").hide()表示display:none; $("#id").toggle()切换元素的可见状态.如果元素是可见的,切换为隐藏的

radio与checkbox

最近一直在学习Javascript与asp.net MVC4,每天都在跟着书学习.这样总感觉自己看的很抽象,没有点实际的意义.而且,每次看的东西很容易忘记,所以打算在这里记录自己的学习笔记. Javascript已经看了不少天了,本文主要讲述的是radio与checkbox. 1.radio radio又称单选框,它是html表单中的单选按钮.通常单选按钮是成组出现的,是互斥的,每次只能选取一个.当点击按钮的时候,就会触发onclick事件.通过属性checked判断按钮是否被选中.声明的语法如

radio单选按钮组操作

radio单选按钮组操作与checkbox复选组操作不同, 因为在一组单选按钮中, 你只要选择了其中一个(表现为checked=ture), 那么其他按钮会自动设置为fasle, 这里不留意就会产生bug, 关键是要搞清楚流程的控制. 比如现在有一个需求是通过一个toggle开关来动态切换两个按钮: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"&g

jquery实用应用之jquery操作radio、checkbox、select

本文收集一些jquery的实用技巧,非常实用的哦,其中对radio.checkbox.select选中与取值的方法. 获取一组radio被选中项的值var item = $('input[@name=items][@checked]').val();获取select被选中项的文本var item = $("select[@name=items] option[@selected]").text();select下拉框的第二个元素为当前选中值$('#select_id')[0].sele

jquery循环遍历radio单选按钮,并设置选中状态

背景:自己在做项目过程中遇到的问题,现在记录一下. 需求:在ajax获取后台数据的之后,需要根据获取的数据对页面中的radio单选按钮进行选中状态设置 因为自身js功底欠佳,所以耽误了点时间,现在把方法写一下 先贴一下html代码,这里就以最简单的代码来演示: <input type="radio" class="optionsRadios" value="1">是 <input type="radio" c

js如何获取选中radio单选按钮的值

js如何获取选中radio单选按钮的值:radio单选按钮在是非常常用的表单元素之一,经常需要获取被选中按钮的value属性值,下面就通过实例简单介绍一下如何使用javascript实现此功能,代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.soft

纯js模拟 radio和checkbox控件

代码待优化,功能实现了,不兼容ie8以上, 相同name的radio可以实现切换的操作, 分享代码,共同学习进步 <!doctype html> <html> <head> <meta charset="utf-8"> <title></title> <style> .radiobox, .checkbox { width: 10px; height: 10px; padding: 2px; borde

JQuery触发radio或checkbox的change事件

在JQuery中,当给radio或checkbox添加一个change事件时,如果它的值发生变化就会触发change事件;本文将详细介绍如何利用JQuery触发Checkbox的change事件需要了解的朋友可以参考下 早上要做一功能,checkbox被选中时,显示隐藏的层,取消选中时,再隐藏选中的层. 初始代码如下: 复制代码代码如下: $(function(){ $("#ischange").change(function() { alert("checked"

jQuery 获取和设置radio 和 checkbox 值的操作

jquery 中的val(),可以取值也可赋值,表单元素中的radio和checkbox是比较常用的控件,下面说说对它们的取值和赋值的使用 1.取值 表单如下: <div class="items"> @if (Model.Question.type == "单选") { <div><input type="radio" id="A" name="sin" value=&quo

前端开发:css技巧,如何设置select、radio 、 checkbox 、file这些不可直接设置的样式 。

前言: 都说程序员有三宝:人傻,钱多,死得早.博主身边的程序“猿”一大半应了这三宝,这从侧面说明了一个问题,只有理性是过不好日子的.朋友们应该把工作与生活分开,让生活变得感性,让工作变得理性,两者相提并行,岂不快哉.咳,话题扯得有点远,今天博主给大家写一篇关于css如何设置select.radio . checkbox .file样式的问题,这里不涉及模拟框,仅介绍原生情况下如何做到自定义样式,废话不多说,赖次够! 1.select与input file: 相信大家都遇到过这样的问题,大多数浏览