Android练习-----单选按钮(RadioButton) && 复选框(CheckBox)

来源:互联网 发布:知秋的小说哪本好看 编辑:程序博客网 时间:2024/05/22 16:51

在Eclipse中创建一个Android项目,名称为TestRadioButtonCheckBox.


布局文件:

<LinearLayout     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:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/sex" />   <RadioGroup         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:id="@+id/sex">       <RadioButton             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/man"            android:id="@+id/radio_man"/>       <RadioButton             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/women"            android:id="@+id/radio_women"/>   </RadioGroup>   <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/fav" />   <CheckBox             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/like1"            android:id="@+id/like1"             />   <CheckBox             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/like2"            android:id="@+id/like2"             />   <CheckBox             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/like3"            android:id="@+id/like3"             />   <CheckBox             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/like4"            android:id="@+id/like4"             />   <CheckBox             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/like5"            android:id="@+id/like5"             />   <Button             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button"            android:id="@+id/button"/></LinearLayout>

在res/values下的strings.xml文件中添加文本框文本的显示内容,代码如下

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">TestRadioButtonCheckBox</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="sex">请选择您的性别</string>    <string name="fav">请选择您的兴趣</string>    <string name="man">男</string>    <string name="women">女</string>    <string name="button">确定</string>    <string name="like1">体育</string>    <string name="like2">美术</string>    <string name="like3">看电影</string>    <string name="like4">上网</string>    <string name="like5">购物</string></resources>

MainActivity.java文件

import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.RadioButton;import android.widget.RadioGroup;import android.app.Activity;public class MainActivity extends Activity {    private RadioGroup sex = null;                                                                                              //声明单选按钮组对象    private CheckBox like1 = null;                                                                                              //声明复选按钮控件对象    private CheckBox like2 = null;    private CheckBox like3 = null;    private CheckBox like4 = null;    private CheckBox like5 = null;    private RadioButton radio = null;                                                                                   //声明单选按钮对象    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //找到关心的控件        Button button = (Button) findViewById(R.id.button);                     //确定按钮        button.setOnClickListener(new buttonOnClickListener());             //为确定按钮添加监听器        sex = (RadioGroup) findViewById(R.id.sex);                                          like1 = (CheckBox) findViewById(R.id.like1);        like2 = (CheckBox) findViewById(R.id.like2);        like3 = (CheckBox) findViewById(R.id.like3);        like4 = (CheckBox) findViewById(R.id.like4);        like5 = (CheckBox) findViewById(R.id.like5);        like1.setOnCheckedChangeListener(new checkBoxListener());        like2.setOnCheckedChangeListener(new checkBoxListener());        like3.setOnCheckedChangeListener(new checkBoxListener());        like4.setOnCheckedChangeListener(new checkBoxListener());        like5.setOnCheckedChangeListener(new checkBoxListener());        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            public void onCheckedChanged(RadioGroup group, int checkedId) {                radio = (RadioButton) findViewById(checkedId);            }        });    }    class checkBoxListener implements OnCheckedChangeListener{        public void onCheckedChanged(CompoundButton buttonView,                boolean isChecked) {        }    }    class buttonOnClickListener implements OnClickListener{        public void onClick(View v) {            if (radio != null) {                                                                                        //单选按钮是否为空                Log.i("您选择的性别是:", radio.getText().toString());            }else {                Log.i("您选择的性别是:", "无" );            }            String like = "";            if (like1.isChecked()) {                                                                                //复选框是否被选中                like = like + like1.getText().toString() + "  ";            }            if (like2.isChecked()) {                                                                                //复选框是否被选中                like = like + like2.getText().toString() + "  ";            }            if (like3.isChecked()) {                                                                                //复选框是否被选中                like = like + like3.getText().toString() + "  ";            }            if (like4.isChecked()) {                                                                                //复选框是否被选中                like = like + like4.getText().toString() + "  ";            }            if (like5.isChecked()) {                                                                                //复选框是否被选中                like = like + like5.getText().toString() + "  ";            }            if ("".equals(like.trim())) {                                                                           //没有复选框被选中                    Log.i("您的兴趣是:", "无");            }   else {                    Log.i("您的兴趣是:", like);            }        }    }}
0 0
原创粉丝点击