android radio单选切换,Android RadioButton使用单选框的方法

很多小伙伴们在Android RadioButton中不知道怎么使用单选框,那么今天爱站小编就将为大家介绍Android RadioButton使用单选框的方法,感兴趣的小伙伴们一起看看吧。

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中的项只能选择一项;

android:id="@+id/gendergroup"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="vertical" >

android:id="@+id/girl"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

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

android:id="@+id/boy"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

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

以上就是小编为大家介绍Android RadioButton使用单选框的方法,其实还有很多很多技巧,小编在这里就不一一列举出来了,还有这里的代码可能不全面,有问题的话大家可以留言交流。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值