android RadioGroup实现单选以及默认选中

本文将通过radiogroup和radiobutton实现组内信息的单选,

其中radiogroup就是将radiobutton进行分组,同一管理和控制

同时实现默认选中情况,获取默认值.效果图

具体实例如下:

1.activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/diplay_seleted_item_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/please_selected"/>

    <RadioGroup
        android:id="@+id/sex_rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#00FF00"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/man_rb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/man" />

        <RadioButton
            android:id="@+id/woman_rb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/woman" />
    </RadioGroup>

</LinearLayout>

2.strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">RadioGroupDemo</string>
    <string name="please_selected">请选择你的性别:</string>
    <string name="action_settings">Settings</string>
    <string name="man">男</string>
    <string name="woman">女</string>

</resources>

3.MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        defaultStr = this.getResources().getString(R.string.please_selected);
        seletedTv = (TextView) this.findViewById(R.id.diplay_seleted_item_tv);
        sexRg = (RadioGroup) this.findViewById(R.id.sex_rg);
        manRb = (RadioButton) this.findViewById(R.id.man_rb);
        womanRb = (RadioButton) this.findViewById(R.id.woman_rb);

        manRb.setChecked(true);
        seletedTv.setText(defaultStr + manRb.getText().toString());

        sexRg.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup rg, int checkedId) {
				// TODO Auto-generated method stub
				if(checkedId == manRb.getId()){
					seletedTv.setText(defaultStr + manRb.getText().toString());
				}else if(checkedId == womanRb.getId()){
					seletedTv.setText(defaultStr + womanRb.getText().toString());
				}else{
					seletedTv.setText(defaultStr);
				}
			}
        });
    }

android RadioGroup实现单选以及默认选中

时间: 2024-07-30 06:38:42

android RadioGroup实现单选以及默认选中的相关文章

Jquery默认选中单选框radio第一个、选中指定值的单选框

概述 当页面加载时,指定区域的单选框默认选中第一个:用户勾选单选框之后,再次回到页面时,需要选中上次勾选的单选框 JS 选中指定id为"admin-content-task"中的某个radio // 页面隐藏域的值 var platIdVal = $("#platIdVal").val(); // 如果platIdVal为空,说明没有勾选单选框 if(platIdVal==null || platIdVal == ''){ // 默认选中平台单选框的第一个 $(&q

Android ListView 默认选中某一项

这里是使用 TOC 生成的目录: Layout文件定义 ListView定义 item 模板定义 代码 初始化列表 用户点击处理 效果 要使用 ListView 实现一个充值方式选择,默认想选中第二项,搞了一下午,终于搞定了.原本就没怎么用 Java 写过 Android 应用,又隔了好久没写,一切都生疏了,半吊子变成大呆瓜了-- Layout文件定义 分两部分,一部分是 ListView 的定义,一部分 item 模板,即 row 的定义. ListView定义 说起来也很简单,下面是 Lay

Android自定义单选,自定义选中状态

如图,此布局用GrildView实现,弹出框由Activity的dialog样式实现. 屏蔽系统GrildView点击背景黄色: grildview.setSelector(new ColorDrawable(Color.TRANSPARENT)); 实现数据源自定义Adapter public class PeoPleNumAdapter extends BaseAdapter { public List<PeopleNum> FiltArray; public static HashMap

android RadioGroup中设置selector后出现多个别选中的RadioButton的解决办法

在一个RadioGroup组中假如有三个或者以上的RadioButton,当然你需要给这些RadioButton设置selector.设置其中的一个为默认选中状态(在xml中设置).当程序在手机上运行时你会发现,那个设置默认选中的RadioButton总是被选中的. 那么怎样解决它呢? 办法:既然在xml设置默认选中状态的方法行不通,那么我们就换一种思路,在代码中动态的设置RadioButton的checked属性.这就是问题的解决办法,就是这么的简单.愿意暂时不详.(不过这个结论我已经证实了,

Android RadioGroup和RadioButton详解

实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中:当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个.并用setOnCheckedChangeListener来对单选按钮进行监听 1 RadioGroup相关属性: 2 RadioGroup.getC

如何在Android的ListView中构建CheckBox和RadioButton列表(Android版支持单选和多选的投票项目)

引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本文以一个支持单选和多选投票项目为例,演示了在一个ListView中如何构建CheckBox列表和RadioButton列表,并分析了实现的原理和思路,提供有需要的朋友参考. 项目的演示效果如下. 数据源 通常我们的数据源来自于数据库.首先,我们构建投票项目类SubjectItem. /** * 主题

Android RadioGroup RadioButton的一个小技巧

1.先上代码 <RadioGroup android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_feature" android:layout_width="match_pare

Dojo Tree设置默认选中项并且获得它

在使用CSS3的中有如下类似代码 Html代码 @media screen and (max-width:480px){ -- } 意思是在最大宽度为480px的设备上应用{}里面的样式.这里的width,注意是手机浏览器的分辨率,而不是手机设备的屏幕分辨率.比如苹果4的手机屏幕分辨率是960x640.而其自带的Safari浏览器的分辨率是320*480.我们可以通过如下代码检测所用的浏览器的分辨率: Js代码 $("#info").html("(您的浏览器的分辨率为:&qu

getresources()与Spinner取消默认选中第一项

getResource() getResource是在有context的前提下才能使用,在android中常在activity中使用它.普通的类一般无法使用.否则会报空指针异常. Spinner默认选中第一项 取消默认选中第一项的方法: spinner.setSelection(0, true); 注意: