Radiogroup、Checkbox、Toast

1、values/string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Activity07!</string>
    <string name="app_name">Activity07</string>
    <string name="male">男</string>
    <string name="female">女</string>
    <string name="swim">游泳</string>
    <string name="run">跑步</string>
    <string name="read">读书</string>
</resources>
 

2、layout/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" >

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

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

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

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

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

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

</LinearLayout>
 

3、Activity

package mars.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class Activity07 extends Activity {
	/** Called when the activity is first created. */

	private RadioGroup grenderGroup = null;

	private RadioButton male = null, female = null;
	private CheckBox swim = null, run = null, read = null;

	private int maleId = 0, femaleId = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		grenderGroup = (RadioGroup) super.findViewById(R.id.grenderGroup);
		male = (RadioButton) super.findViewById(R.id.male);
		female = (RadioButton) super.findViewById(R.id.female);
		swim = (CheckBox) super.findViewById(R.id.swim);
		run = (CheckBox) super.findViewById(R.id.run);
		read = (CheckBox) super.findViewById(R.id.read);

		grenderGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						if (checkedId == male.getId()) {
							System.out.println("Male");
						} else if (checkedId == female.getId()) {
							System.out.println("Female");
						}
					}
				});
		swim.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					System.out.println("Swimming");
				}
			}
		});
		run.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					System.out.println("Running");
					//吐司,就把它理解为弹出来的意思
					Toast.makeText(Activity07.this, "Running", Toast.LENGTH_SHORT).show();
				}
			}
		});
		read.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (isChecked) {
					System.out.println("Readding");
				}
			}
		});
	}
}

 d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值