Android中ImageButton、RadioGroup及CheckBox的使用(五)

package com.itarchy.demo;

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

public class MainActivity extends Activity implements OnCheckedChangeListener {

	private ImageButton imgBtn;

	private RadioGroup mRadioGroup;
	private CheckBox cbGame;
	private CheckBox cbStudy;
	private CheckBox cbRead;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();

	}

	private void initView() {

		// RadioGroup
		mRadioGroup = (RadioGroup) this.findViewById(R.id.rg);
		mRadioGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						switch (checkedId) {
						case R.id.sex_man:
							Toast.makeText(getApplication(), "男", 1000).show();
							break;
						case R.id.sex_men:
							Toast.makeText(getApplication(), "女", 1000).show();
							break;
						}
					}
				});

		// 2、CheckBox
		cbGame = (CheckBox) this.findViewById(R.id.like_game);
		cbGame.setOnCheckedChangeListener(this);

		cbStudy = (CheckBox) this.findViewById(R.id.like_study);
		cbStudy.setOnCheckedChangeListener(this);

		cbRead = (CheckBox) this.findViewById(R.id.like_read);
		cbRead.setOnCheckedChangeListener(this);
	}

	// CheckBox实现监听器时执行的方法
	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

		switch (buttonView.getId()) {
		case R.id.like_game:
			Toast.makeText(getApplication(), isChecked ? "喜欢游戏" : "不喜欢游戏", 1000)
					.show();
			break;
		case R.id.like_study:
			Toast.makeText(getApplication(), isChecked ? "喜欢学习" : "不喜欢学习", 1000)
					.show();
			break;
		case R.id.like_read:
			Toast.makeText(getApplication(), isChecked ? "喜欢阅读" : "不喜欢阅读", 1000)
					.show();
			break;

		}

	}

}
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world" />

    <ImageButton
        android:id="@+id/imgbtn"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/my_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imgbtn"
        android:layout_marginTop="15dip"
        android:text="请选择您的性别:" />

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_sex"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/sex_man"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男" />

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

    <TextView
        android:id="@+id/my_fav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rg"
        android:layout_marginTop="15dip"
        android:text="请选择您的爱好:" />

    <LinearLayout
        android:id="@+id/ll_fav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/my_fav"
        android:orientation="horizontal" >

        <CheckBox
            android:id="@+id/like_game"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/checkbox"
            android:text="游戏" />

        <CheckBox
            android:id="@+id/like_study"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/checkbox"
            android:text="学习" />

        <CheckBox
            android:id="@+id/like_read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/checkbox"
            android:text="阅读" />
    </LinearLayout>

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itarchy.demo.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、付费专栏及课程。

余额充值