服务器之家

服务器之家 > 正文

Android程序开发中单选按钮(RadioGroup)的使用详解

时间:2021-06-22 15:23     来源/作者:Android开发网

在还没给大家介绍单选按钮(radiogroup)的使用,先给大家展示下效果图吧:

Android程序开发中单选按钮(RadioGroup)的使用详解

xml文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<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"
tools:context=".mainactivity"
android:orientation="vertical">
<textview
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="您的性别为"/>
<radiogroup
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<radiobutton
android:id="@+id/male"
android:text="男"/>
<radiobutton
android:id="@+id/female"
android:text="女"/>
</radiogroup>
</linearlayout>

java文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class
mainactivity extends activity {
private textview txt=null;
private radiogroup sex=null;
private radiobutton male=null;
private radiobutton female=null;
@override
protected void
oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
this.txt=(textview)
super.findviewbyid(r.id.txt);
this.sex=(radiogroup)
super.findviewbyid(r.id.sex);
this.male=(radiobutton)
super.findviewbyid(r.id.male);
this.female=(radiobutton)
super.findviewbyid(r.id.female);
this.sex.setoncheckedchangelistener(new
oncheckedchangelistenerimp());
} private class
oncheckedchangelistenerimp implements
oncheckedchangelistener{
public void
oncheckedchanged(radiogroup group, int checkedid)
{ string temp=null;
if(mainactivity.this.male.getid()==checkedid){
temp="男";
} else if(mainactivity.this.female.getid()==checkedid){
temp="女";
} mainactivity.this.txt.settext("您的性别是"+temp);
} }

以上所述是小编给大家介绍的android程序开发中单选按钮(radiogroup)的使用详解,希望对大家有所帮助!

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部