在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
在还没给大家介绍单选按钮(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
|
<linearlayoutxmlns: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 classmainactivity extends activity { private textview txt=null; private radiogroup sex=null; private radiobutton male=null; private radiobutton female=null; @overrideprotected voidoncreate(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(newoncheckedchangelistenerimp()); } private classoncheckedchangelistenerimp implementsoncheckedchangelistener{ public voidoncheckedchanged(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)的使用详解,希望对大家有所帮助!
发表评论