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>