Android学习笔记之RadioButton RadioGroup

Android学习笔记之RadioButton RadioGroup
摘要由CSDN通过智能技术生成

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                RadioButton(单选按钮)在Androi发中应用的非常广泛,比如一些选择项的时候,会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在RadioButton没有被选中时,用户能够按下或点击来选中它。但是,与复选框相反,用户一旦选中就不能够取消选中。

    实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听

main.xml

<?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/sex"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/sex" />    <RadioGroup         android:id="@+id/radiogroup"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >        <RadioButton             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/female"            android:text="@string/female"            ></RadioButton>        <RadioButton             android:id="@+id/male"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/male"            android:checked="true"            ></RadioButton>    </RadioGroup></LinearLayout>


 

RadioGroupActivity.java

通过控件的ID来得到代表控件的对象

然后为RadioGroup设置监听器

package Android.Activity;import android.app.Activity;import android.os.Bundle;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class RadioGroupActivity extends Activity {    /** Called when the activity is first created. */ private RadioButton maleButton = nullprivate RadioButton femaleButton = nullprivate RadioGroup radiogroup =null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        //通过控件的ID来得到代表控件的对象        maleButton = (RadioButton)findViewById(R.id.male);        femaleButton = (RadioButton)findViewById(R.id.female);        radiogroup = (RadioGroup)findViewById(R.id.radiogroup);        //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {      public void onCheckedChanged(RadioGroup group, int checkedId) {    // TODO Auto-generated method stub    if(femaleButton.getId() == checkedId){     Toast.makeText(RadioGroupActivity.this, "female", Toast.LENGTH_SHORT).show();    }    else if(maleButton.getId() == checkedId){     Toast.makeText(RadioGroupActivity.this, "male",
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值