我只是在此做个记录。。。
原文链接:点击打开链接http://liangruijun.blog.51cto.com/3061169/652042/
- package com.android.radiobutton;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.RadioGroup;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- //声明RadioGroup
- RadioGroup raGroup1,raGroup2;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //通过findViewById获得RadioGroup对象
- raGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
- //添加事件监听器
- raGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- if(checkedId==R.id.radioBtn1){
- Toast.makeText(MainActivity.this, "你来自广东省", Toast.LENGTH_LONG).show();
- }
- else if(checkedId==R.id.radioBtn2){
- Toast.makeText(MainActivity.this, "你来自广西省", Toast.LENGTH_LONG).show();
- }
- else{
- Toast.makeText(MainActivity.this, "你来自湖南省", Toast.LENGTH_LONG).show();
- }
- }
- });
- raGroup2=(RadioGroup)findViewById(R.id.radioGroup2);
- raGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- // TODO Auto-generated method stub
- if(checkedId==R.id.radioBtn4){
- Toast.makeText(MainActivity.this, "你的性别是男", Toast.LENGTH_LONG).show();
- }
- else {
- Toast.makeText(MainActivity.this, "你的性别是女", Toast.LENGTH_LONG).show();
- }
- }
- });
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello1"
- />
- <RadioGroup
- android:id="@+id/radioGroup1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <RadioButton
- android:id="@+id/radioBtn1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn1"
- />
- <RadioButton
- android:id="@+id/radioBtn2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn2"
- />
- <RadioButton
- android:id="@+id/radioBtn3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn3"
- />
- </RadioGroup>
- <!-- 在两个RadioGroup之间画条横线 -->
- <View
- android:layout_width="match_parent"
- android:layout_height="1dp"
- android:background="#ffffff"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello2"
- />
- <RadioGroup
- android:id="@+id/radioGroup2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <RadioButton
- android:id="@+id/radioBtn4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn4"
- android:textColor="#ffffff"
- />
- <RadioButton
- android:id="@+id/radioBtn5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn5"
- />
- </RadioGroup>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello1">你来自哪个省</string>
- <string name="hello2">你的性别是</string>
- <string name="app_name">单选按钮测试</string>
- <string name="radioBtn1">广东</string>
- <string name="radioBtn2">广西</string>
- <string name="radioBtn3">湖南</string>
- <string name="radioBtn4">男</string>
- <string name="radioBtn5">女</string>
- </resources>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello1"
- />
- <RadioGroup
- android:id="@+id/radioGroup1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <RadioButton
- android:id="@+id/radioBtn1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn1"
- android:button="@null"
- android:drawableRight="@android:drawable/btn_radio"
- />
- <RadioButton
- android:id="@+id/radioBtn2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn2"
- android:button="@null"
- android:drawableRight="@android:drawable/btn_radio"
- />
- <RadioButton
- android:id="@+id/radioBtn3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn3"
- android:button="@null"
- android:drawableRight="@android:drawable/btn_radio"
- />
- </RadioGroup>
- <!-- 在两个RadioGroup之间画条横线 -->
- <View
- android:layout_width="match_parent"
- android:layout_height="1dp"
- android:background="#ffffff"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello2"
- />
- <RadioGroup
- android:id="@+id/radioGroup2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:orientation="vertical"
- >
- <RadioButton
- android:id="@+id/radioBtn4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn4"
- android:textColor="#ffffff"
- android:button="@null"
- android:drawableRight="@android:drawable/btn_radio"
- />
- <RadioButton
- android:id="@+id/radioBtn5"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/radioBtn5"
- android:button="@null"
- android:drawableRight="@android:drawable/btn_radio"
- />
- </RadioGroup>
- </LinearLayout>
- package com.android.checkbox;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.Toast;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- public class MainActivity extends Activity{
- //声明复选按钮
- private CheckBox cBox1;
- private CheckBox cBox2;
- private CheckBox cBox3;
- @Override
- public void onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //通过findViewById获得CheckBox对象
- cBox1=(CheckBox)findViewById(R.id.checkbox1);
- cBox2=(CheckBox)findViewById(R.id.checkbox2);
- cBox3=(CheckBox)findViewById(R.id.checkbox3);
- //注册事件监听器
- cBox1.setOnCheckedChangeListener(listener);
- cBox2.setOnCheckedChangeListener(listener);
- cBox3.setOnCheckedChangeListener(listener);
- }
- //响应事件
- private OnCheckedChangeListener listener = new OnCheckedChangeListener(){
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
- {
- //cBox1被选中
- if (buttonView.getId()==R.id.checkbox1){
- if (isChecked){
- Toast.makeText(MainActivity.this, "你喜欢足球", Toast.LENGTH_LONG).show();
- }
- }
- //cBox2被选中
- else if (buttonView.getId()==R.id.checkbox2){
- if (isChecked){
- Toast.makeText(MainActivity.this, "你喜欢篮球", Toast.LENGTH_LONG).show();
- }
- }
- //cBox3被选中
- else if (buttonView.getId()==R.id.checkbox3){
- if (isChecked){
- Toast.makeText(MainActivity.this, "你喜欢排球", Toast.LENGTH_LONG).show();
- }
- }
- }
- };
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- android:textSize="20sp"
- android:textStyle="bold"
- android:textColor="#FFFFFF"
- />
- <CheckBox
- android:id="@+id/checkbox1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/football"
- android:textSize="16sp"
- />
- <CheckBox
- android:id="@+id/checkbox2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/basketball"
- android:textSize="16sp"
- />
- <CheckBox
- android:id="@+id/checkbox3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/volleyball"
- android:textSize="16sp"
- />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">你喜欢的运动是</string>
- <string name="app_name">复选按钮测试</string>
- <string name="football">足球</string>
- <string name="basketball">篮球</string>
- <string name="volleyball">排球</string>
- </resources>