关于如何在XML中设置RadioButton默认选中

news/2024/7/3 2:30:16

首先我遇到的问题是:

                <RadioGroup
                    android:id="@+id/radioGroup"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="商圈" />

                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:text="社区" />

                    <RadioButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="大学" />
                </RadioGroup>


当运行时候,社区永远是被选中的,不是我想要的结果。

解决方案:需要给RadioButton添加id属性,这样默认选中才会生效。如:

                <RadioGroup
                    android:id="@+id/propertyRadioGroup"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <RadioButton
                        android:id="@+id/propertyRadioButton1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="商圈" />

                    <RadioButton
                        android:id="@+id/propertyRadioButton2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:text="社区" />

                    <RadioButton
                        android:id="@+id/propertyRadioButton3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="大学" />
                </RadioGroup>

OK!问题解决了。

补充:后台如何获取选中的RadioButton:

					int radioButtonId=propertyRadioGroup.getCheckedRadioButtonId();
					RadioButton radioButton = (RadioButton)propertyRadioGroup.findViewById(radioButtonId);
					handler.sendToastMessage("text:"+radioButton.getText().toString());
					handler.sendToastMessage("hint:"+radioButton.getHint().toString());





http://www.niftyadmin.cn/n/1967386.html

相关文章

IFE-24 笔记

1、捕获event.target事件&#xff0c;以及移除捕获的节点使其变色,设置为全局变量&#xff0c;其他函数中也可以调用 //选中某个框点击使其变色treeRoot.addEventListener(click,btnChangeC,false);function btnChangeC(event){//先清除上一次的变色reset();event.target.style…

简易ATM系统练习

执行简单的ATM功能&#xff1a;主要练习JavaOO参考了阿里巴巴java内部规范&#xff0c;希望自己所书写的程序能够更加的规范性&#xff1b;package com.lovo.ATM.bean;import java.util.Scanner; /*** ATM系统类* author 侯熙* version 1.1* since jdk1.8.0_25*/ public class …

EL:Expression language

在传统的jsp页面中&#xff0c;使用<% java代码 %>显示数据有许多的弊端。 比如类型转换&#xff0c;需要处理的null&#xff0c;代码参杂。 采用EL,JSTL等就方便了很多。 EL&#xff1a; 实例&#xff1a; ${requestScope.student.address.schoolAddress} 这里是request…

android 输入框 XML 设置于代码设置异同

android InputType 参数对应代码设置 详见android SDK android XML设置EditText输入格式参数对应constant列 android:inputTypeXXX android java代码设置EditText输入格式参数对应Description列 setInputType(XXXXXXXX)或者setRawInputType(XXXXXXXX) 需要注意XML部分参数值…

c++中的header-only library

不同于在java中&#xff0c;虽然在java中&#xff0c;有些第三方库只是做了桥接的功能&#xff0c;比如slf4j-log4j-api&#xff0c;但是在运行时他们仍然是需要的&#xff0c;所以最多只能说是松耦合做得很好。 但是在c中&#xff0c;一般我们应用第三方库的时候&#xff0c;是…

过滤器和拦截器,监听器

过滤器&#xff08;filter&#xff09;&#xff1a;请求&#xff0c;响应&#xff0c;需要拦截器放行。 实现一个接口&#xff0c;init(初始化)&#xff0c;destroy&#xff08;摧毁&#xff09;,需要在xml中配置过滤器。 配置过滤器&#xff0c;同配置servlet一样。 <filt…

经纬度距离搜索数据库操作以及hibernate操作、Java比较两点间的距离

数据库语句&#xff1a; select * from ls_region_user order by ACOS(SIN((39.975092 * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) COS((39.975092 * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((116.385476 * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6378.…

ajax传输

ajax:异步传输jsxml 异步刷新&#xff1a;网页中只有某一块需要修改&#xff0c;不影响其他块。比如说弹幕&#xff0c;点赞等&#xff01; 实现&#xff1a; js: XMLHttpRequest 对象&#xff1b; 该对象的方法&#xff1a; open( 提交方式,服务器地址 ,true):与服务器建立连…