android RadioGroup的使用方法
程序员文章站
2023-12-15 21:29:58
创建一个mainactivity.java的主类 复制代码 代码如下:
创建一个mainactivity.java的主类
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<textview
android:id="@+id/radiobutton_textview"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textsize="18dip"
android:textstyle="bold"
android:background="@android:drawable/title_bar"
android:textappearance="?android:attr/textappearancelarge"
android:gravity="center_vertical"
/>
<radiogroup
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<radiobutton
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textsize="20dip"
android:paddingleft="30dip"
android:text="android新手"
android:button="@null"
android:drawableright="@android:drawable/btn_radio"/>
<view
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="?android:attr/listdivider"
/>
<radiobutton
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textsize="20dip"
android:paddingleft="30dip"
android:text="android高手"
android:button="@null"
android:drawableright="@android:drawable/btn_radio"/>
</radiogroup>
</linearlayout>
xml代码
package endual.radio;
import android.app.activity;
import android.os.bundle;
import android.widget.radiobutton;
import android.widget.radiogroup;
import android.widget.radiogroup.oncheckedchangelistener;
import android.widget.textview;
public class mainactivity extends activity {
private textview textview;
private radiogroup group;
private radiobutton rb1 ;
private radiobutton rb2 ;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
textview = (textview) findviewbyid(r.id.radiobutton_textview);
group = (radiogroup) findviewbyid(r.id.group);
this.rb1 = (radiobutton) this.findviewbyid(r.id.button1) ;
this.rb2 = (radiobutton) this.findviewbyid(r.id.button2) ;
// 单选按钮组监听事件
group.setoncheckedchangelistener(new oncheckedchangelistener() {
@override
public void oncheckedchanged(radiogroup group, int checkedid) {
// 根据id判断选择的按钮
if (checkedid == r.id.button1) {
textview.settext("android新手");
rb1.settext("我是1") ;
string msg = rb1.gettext().tostring() ; //获取单独的radiobutton的按钮
rb2.settext(msg) ;
//system.out.println();
} else {
textview.settext("android高手");
rb2.settext("我是2") ;
}
}
});
}
}
radiogroup注册监听事件oncheckedchangelistener(),在oncheckedchanged实现业务逻辑。
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<textview
android:id="@+id/radiobutton_textview"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textsize="18dip"
android:textstyle="bold"
android:background="@android:drawable/title_bar"
android:textappearance="?android:attr/textappearancelarge"
android:gravity="center_vertical"
/>
<radiogroup
android:id="@+id/group"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<radiobutton
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textsize="20dip"
android:paddingleft="30dip"
android:text="android新手"
android:button="@null"
android:drawableright="@android:drawable/btn_radio"/>
<view
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="?android:attr/listdivider"
/>
<radiobutton
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:textsize="20dip"
android:paddingleft="30dip"
android:text="android高手"
android:button="@null"
android:drawableright="@android:drawable/btn_radio"/>
</radiogroup>
</linearlayout>
xml代码
复制代码 代码如下:
package endual.radio;
import android.app.activity;
import android.os.bundle;
import android.widget.radiobutton;
import android.widget.radiogroup;
import android.widget.radiogroup.oncheckedchangelistener;
import android.widget.textview;
public class mainactivity extends activity {
private textview textview;
private radiogroup group;
private radiobutton rb1 ;
private radiobutton rb2 ;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
textview = (textview) findviewbyid(r.id.radiobutton_textview);
group = (radiogroup) findviewbyid(r.id.group);
this.rb1 = (radiobutton) this.findviewbyid(r.id.button1) ;
this.rb2 = (radiobutton) this.findviewbyid(r.id.button2) ;
// 单选按钮组监听事件
group.setoncheckedchangelistener(new oncheckedchangelistener() {
@override
public void oncheckedchanged(radiogroup group, int checkedid) {
// 根据id判断选择的按钮
if (checkedid == r.id.button1) {
textview.settext("android新手");
rb1.settext("我是1") ;
string msg = rb1.gettext().tostring() ; //获取单独的radiobutton的按钮
rb2.settext(msg) ;
//system.out.println();
} else {
textview.settext("android高手");
rb2.settext("我是2") ;
}
}
});
}
}
radiogroup注册监听事件oncheckedchangelistener(),在oncheckedchanged实现业务逻辑。