本文实例讲述了android编程开发之radiogroup用法。分享给大家供大家参考,具体如下:
radiogroup 有时候比较有用.主要特征是给用户提供多选一机制。
mainactivity.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
34
35
36
|
package com.example.lesson16_radio;
import android.app.activity;
import android.os.bundle;
import android.widget.radiobutton;
import android.widget.radiogroup;
import android.widget.toast;
public class mainactivity extends activity {
private radiogroup group_temo;
private radiobutton checkradiobutton;
@override
protected void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
group_temo = (radiogroup) findviewbyid(r.id.radiogroup1);
// 改变默认选项
group_temo.check(r.id.radio1);
// 获取默认被被选中值
checkradiobutton = (radiobutton) group_temo.findviewbyid(group_temo
.getcheckedradiobuttonid());
toast.maketext( this , "默认的选项的值是:" + checkradiobutton.gettext(),
toast.length_long).show();
// 注册事件
group_temo
.setoncheckedchangelistener( new radiogroup.oncheckedchangelistener() {
@override
public void oncheckedchanged(radiogroup group, int checkedid) {
// 点击事件获取的选择对象
checkradiobutton = (radiobutton) group_temo
.findviewbyid(checkedid);
toast.maketext(getapplicationcontext(),
"获取的id是" + checkradiobutton.gettext(),
toast.length_long).show();
}
});
}
}
|
布局文件
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
34
|
<relativelayout 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"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= ".mainactivity" >
<radiogroup
android:id= "@+id/radiogroup1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignparentleft= "true"
android:layout_alignparentright= "true"
android:layout_alignparenttop= "true" >
<radiobutton
android:id= "@+id/radio0"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:checked= "true"
android:text= "@string/text_java" />
<radiobutton
android:id= "@+id/radio1"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "@string/text_net" />
<radiobutton
android:id= "@+id/radio2"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:text= "@string/text_php" />
</radiogroup>
</relativelayout>
|
希望本文所述对大家android程序设计有所帮助。