RadioGroup RadioButton的Demo

RadioButton和CheckBox的区别:

1、单个RadioButton在选中后,通过点击无法变为未选中

      单个CheckBox在选中后,通过点击可以变为未选中

2、一组RadioButton,只能同时选中一个

     一组CheckBox,能同时选中多个

3、RadioButton在大部分UI框架中默认都以圆形表示

     CheckBox在大部分UI框架中默认都以矩形表示

RadioButton和RadioGroup的关系:

1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器

2、每个RadioGroup中的RadioButton同时只能有一个被选中

3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中

4、大部分场合下,一个RadioGroup中至少有2个RadioButton

5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置

 
效果图:
 
 
===================文件名:activity_main.xml===================
<LinearLayout 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:background= "@drawable/main_bg"
    android:orientation= "vertical" >

    <TextView
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content"
        android:text= "@string/chosesex"
        tools:context= ".MainActivity" />

    <RadioGroup
        android: id= "@+id/main_choseSex"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content" >

        <RadioButton
            android: id= "@+id/radioBoy"
            android:layout_width= "wrap_content"
            android:layout_height= "wrap_content"
            android:checked= "true"
            android:text= "@string/radaoBoy" />

        <RadioButton
            android: id= "@+id/radioGirls"
            android:layout_width= "wrap_content"
            android:layout_height= "wrap_content"
            android:text= "@string/radioGirls" />
    </RadioGroup>

    <TextView
        android: id= "@+id/choseContent"
        android:layout_width= "wrap_content"
        android:layout_height= "wrap_content" />

</LinearLayout>
===================文件名:AndroidManifest.xml===================
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
    package= "com.ygc.radiobutton.activity"
    android:versionCode= "1"
    android:versionName= "1.0" >

    <uses-sdk
        android:minSdkVersion= "8"
        android:targetSdkVersion= "15" />

    <application
        android:icon= "@drawable/ic_launcher"
        android:label= "@string/app_name"
        android:theme= "@style/AppTheme" >
        <activity
            android:name= ".MainActivity"
            android:screenOrientation= "portrait"
            android:label= "@string/title_activity_main" >
            <intent-filter>
                <action android:name= "android.intent.action.MAIN" />

                <category android:name= "android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 ===================文件名:MainActivity.java===================
package com.ygc.radiobutton.activity;

import android.app.Activity;
import android.os.Bundle;
import android. view. Menu;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
/**
*
* <p>Title: MainActivity.java</p>
* <p>Description: 介绍了Android中如何使用RadioGroup和RadioButton,
* 对比了RadioButton和CheckBox的区别,并实现了自定义的RadioGroup中被
* 选中RadioButton的变更监听事件。</p>
* <p>Copyright: Copyright (c) 2011</p>
* <p>Company: ygc Teachnology</p>
* <p>CreateTime: 2012-10-9 下午12:39:59</p>
*
* @author ygc
* @version %A%, %B%
* @since JDK1.6
* @CheckItem null
*/
public class MainActivity extends Activity {
    private RadioGroup choseSex;
    private RadioButton radioBoy;
    private RadioButton radioGirls;
    private TextView choseContent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        choseSex = (RadioGroup) findViewById(R.id.main_choseSex);
        radioBoy = (RadioButton) findViewById(R.id.radioBoy);
        radioGirls = (RadioButton) findViewById(R.id.radioGirls);
        choseContent = (TextView) findViewById(R.id.choseContent);
        if (radioBoy.isChecked()) {
            choseContent.setText( "你的性别是:" + radioBoy.getText());
        } else {
            choseContent.setText( "你的性别是:" + radioGirls.getText());
        }

        choseSex.setOnCheckedChangeListener( new RadioGroup.OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == radioBoy.getId()) {
                    choseContent.setText( "你的性别是:" + radioBoy.getText());
                } else {
                    choseContent.setText( "你的性别是:" + radioGirls.getText());
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu( Menu menu) {
        getMenuInflater().inflate(R. menu.activity_main, menu);
        return true;
    }

}
 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值