服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android程序开发中单选按钮(RadioGroup)的使用详解

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

2021-06-22 15:23Android开发网 Android

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧

在还没给大家介绍单选按钮(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)的使用详解,希望对大家有所帮助!

延伸 · 阅读

精彩推荐