欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android RadioButton单选框的使用方法

程序员文章站 2023-12-11 23:04:22
复制代码 代码如下:public class mainactivity extends activity {  public radiogroup mradio...

复制代码 代码如下:

public class mainactivity extends activity {

 public radiogroup mradiogroup1;
 public radiobutton mradio1, mradio2;

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);

  mradiogroup1 = (radiogroup) findviewbyid(r.id.gendergroup);
  mradio1 = (radiobutton) findviewbyid(r.id.girl);
  mradio2 = (radiobutton) findviewbyid(r.id.boy);
  mradiogroup1.setoncheckedchangelistener(radiogpchange);

 }

 private radiogroup.oncheckedchangelistener radiogpchange = new radiogroup.oncheckedchangelistener() {
  @override
  public void oncheckedchanged(radiogroup group, int checkedid) {
   if (checkedid == mradio1.getid()) {
    toast.maketext(getapplicationcontext(), "女孩", 1).show();
   } else if (checkedid == mradio2.getid()) {
    toast.maketext(getapplicationcontext(), "男孩", 1).show();
   }
  }
 };
}


radiobutton:就像是c#中的radio控件,可以为控件设置group,每个group中的项只能选择一项;
复制代码 代码如下:

    <radiogroup

        android:id="@+id/gendergroup"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical" >

        <radiobutton

            android:id="@+id/girl"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/girl" />

        <radiobutton

            android:id="@+id/boy"

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@string/boy" />

    </radiogroup>

上一篇:

下一篇: