--------------------------------------------strings.xml--------------------------------------------
<resources>
<string name="app_name">com.ch03</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">com.ch03</string>
<string name="gender">性别</string>
<string name="female">女</string>
<string name="male">男</string>
<string name="lovely">爱好</string>
<string name="food">美食</string>
<string name="music">音乐</string>
<string name="btn">注册</string>
</resources>
--------------------------------------------activity_main.xml-------------------------------------
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/gender" />
<RadioGroup
android:id="@+id/general"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/male"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/male" />
<RadioButton
android:id="@+id/female"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/female" />
</RadioGroup>
<TextView
android:layout_width="fill_parent"
android:layout_height="1sp"
android:background="#000000" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/lovely" />
<CheckBox
android:id="@+id/food"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/food" />
<CheckBox
android:id="@+id/music"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/music" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn" />
</LinearLayout>
--------------------------------------------MainActivity--------------------------------------------
package com.ch03;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.Toast;
/**
*
* 项目名称:com.ch03
* 类名称:MainActivity
* 类描述: 单选按钮与复选按钮
* 创建人:fy
* 创建时间:2012-11-2 下午10:35:02
* Copyright (c) 方勇-版权所有
*/
public class MainActivity extends Activity {
/* 单选按钮 */
private RadioGroup radiog_geneal;
/* 复选,美食 */
private CheckBox check_food;
/* 复选,音乐 */
private CheckBox check_music;
/* 注册按钮 */
private Button btn_register;
/* 性别 */
private String gender;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
setListeners();
}
/* 初始化UI控件 */
private void findViews() {
radiog_geneal = (RadioGroup) findViewById(R.id.general);
check_food = (CheckBox) findViewById(R.id.food);
check_music = (CheckBox) findViewById(R.id.music);
btn_register = (Button) findViewById(R.id.btn);
}
/* 设置监听器 */
private void setListeners() {
btn_register.setOnClickListener(onClickListener);
radiog_geneal.setOnCheckedChangeListener(onCheckedChangeListener);
}
/* 显示结果 */
private void showResults(String values) {
Toast.makeText(MainActivity.this, values, Toast.LENGTH_LONG).show();
}
/* 双击事件 */
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
showResults("性别是:" + gender + "爱好是:" + check_food.getText().toString() + check_music.getText().toString());
}
};
/* 单选按钮选择事件 */
private RadioGroup.OnCheckedChangeListener onCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.male) {
gender = "男";
showResults("您选择的是男");
} else {
gender = "女";
showResults("您选择的是女");
}
}
};
}
<!--EndFragment-->