常用的组件RadioGroup和CheckBox笔记

RadioGroup和CheckBox在Android应用中相对常见,下面就把本人初次学习的笔记放上来。

Activity Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <RadioGroup
        android:id="@+id/sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

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

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <CheckBox
            android:id="@+id/baseball"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/baseball" >
        </CheckBox>

        <CheckBox
            android:id="@+id/basketball"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/basketball" >
        </CheckBox>

        <CheckBox
            android:id="@+id/football"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/football" >
        </CheckBox>

        <CheckBox
            android:id="@+id/swimming"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/swimming" >
        </CheckBox>
    </LinearLayout>

</LinearLayout>

Android Code

package cn.qiuzhping.module1;

import cn.qiuzhping.module1.R;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity {

	private RadioGroup sex = null;
	private RadioButton man = null;
	private RadioButton woman = null;
	private CheckBox baseball = null;
	private CheckBox basketball = null;
	private CheckBox swimming = null;
	private CheckBox football = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		sex = (RadioGroup) findViewById(R.id.sex);
		man = (RadioButton) findViewById(R.id.man);
		woman = (RadioButton) findViewById(R.id.woman);
		baseball = (CheckBox) findViewById(R.id.baseball);
		basketball = (CheckBox) findViewById(R.id.basketball);
		swimming = (CheckBox) findViewById(R.id.swimming);
		football = (CheckBox) findViewById(R.id.football);
		sexSelect();
		hobbySelect();
	}

	public void hobbySelect() {

		baseball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					Log.i("CheckBox", "baseball is Checked");
				} else {
					Log.i("CheckBox", "baseball is not Checked");
				}
			}

		});

		basketball
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) {
							Log.i("CheckBox", "basketball is Checked");
						} else {
							Log.i("CheckBox", "basketball is not Checked");
						}
					}

				});

		swimming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					Log.i("CheckBox", "swimming is Checked");
				} else {
					Log.i("CheckBox", "swimming is not Checked");
				}
			}

		});

		football.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					Log.i("CheckBox", "football is Checked");
				} else {
					Log.i("CheckBox", "football is not Checked");
				}
			}

		});

	}

	public void sexSelect() {

		sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (man.getId() == checkedId) {
					Log.i("onCheckedChanged", "选择man");
				} else if (woman.getId() == checkedId) {
					Log.i("onCheckedChanged", "选择woman");
				} else {
					Log.i("onCheckedChanged", "没有选择");
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.qiuzhping.module1"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="cn.qiuzhping.module1.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值