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