单选按钮和复选框传递数据

创建安卓应用SetBasicInformation
导入图片到drawable目录
在这里插入图片描述
配置字符串资源文件
在这里插入图片描述
在activity_main.xml输入代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/img"
    android:orientation="vertical"
    android:paddingLeft="20dp"
    android:paddingRight="20dp"
    android:paddingTop="50dp"
    >
    <TextView
        android:id="@+id/tv_setinformation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="30dp"
        android:text="@string/set_information"
        android:textColor="#0000ff"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name"
            android:textColor="#000000"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/edt_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/input_name"
            android:singleLine="true"
            android:textSize="20sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gender"
            android:textColor="#000000"
            android:textSize="20sp" />

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

            <RadioButton
                android:id="@+id/rb_male"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="@string/male"
                android:textSize="20sp"/>

            <RadioButton
                android:id="@+id/rb_female"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/female"
                android:textSize="20sp"/>
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_hobby"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hobby"
            android:textColor="#000000"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/music"
            android:textSize="20sp"/>

        <CheckBox
            android:id="@+id/cb_read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/read"
            android:textSize="20sp"/>

        <CheckBox
            android:id="@+id/cb_food"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/food"
            android:textSize="20sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp">

        <Button
            android:id="@+id/btn_ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doOK"
            android:layout_marginRight="10dp"
            android:text="@string/ok"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/btn_clear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doClear"
            android:layout_marginRight="10dp"
            android:text="@string/clear"
            android:textSize="20sp"/>

        <Button
            android:id="@+id/btn_exit"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="doExit"
            android:text="@string/exit"
            android:textSize="20sp"/>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="#dddddd"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_marginTop="30dp"
        android:textSize="20sp"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="10dp"
        android:background="#dddddd"/>

在主界面类 MainActivity输入代码

public class MainActivity extends AppCompatActivity {
    private EditText etName;//姓名编辑框
    private RadioGroup rgGender;//性别单选按钮
    private RadioButton rbMale;//男性单选按钮
    private RadioButton rbFemale;//女性单选按钮
    private CheckBox cbMusic;//音乐复选框
    private CheckBox cbRead;//阅读复选框
    private CheckBox cbFood;//美食复选框
    private TextView tvResult;//结束标签

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 利用布局资源文件设置用户界面
        setContentView(R.layout.activity_main);
        // 通过资源标识符获得控件实例
        etName=findViewById(R.id.edt_name);
        rgGender=findViewById(R.id.rg_gender);
        rbMale=findViewById(R.id.rb_male);
        rbFemale=findViewById(R.id.rb_female);
        cbMusic=findViewById(R.id.cb_music);
        cbRead=findViewById(R.id.cb_read);
        cbFood=findViewById(R.id.cb_food);
        tvResult=findViewById(R.id.tv_result);


//提交按钮单击事件处理方法
            public void doOK(View view) {
                //获取姓名
                String name = etName.getText().toString().trim();
                //获取性别
                String gender = "";
                //判断用户选中那个单选按钮
                switch (rgGender.getCheckedRadioButtonId()) {// 根据选中的单选按钮id进行判断
                    case R.id.rb_male://选中男性单选按钮
                        gender = rbMale.getText().toString();

                        break;
                    case R.id.rb_female:
                        gender = rbFemale.getText().toString();
                        break;
                }
                //获取爱好
                StringBuilder builder = new StringBuilder();//字符串生成器
                //判断用户是否选中了音乐复选框
                if (cbMusic.isChecked()) {
                    builder.append(cbMusic.getText().toString() + "");
                }
                //判断用户是否选中了阅读复选框
                if (cbRead.isChecked()) {
                    builder.append(cbRead.getText().toString() + "");
                }
                //判断用户是否选中了美食复选框
                if (cbFood.isChecked()) {
                    builder.append(cbFood.getText().toString() + "");
                }
                String hobbies = builder.toString().trim();//去掉空格
                //通过标签显示基本信息
                String result = "姓名:" + name + "\n"
                        + "性别" + "gender" + "\n"
                        + "爱好" + hobbies;
                tvResult.setText(result);

            }


            //    清除按钮单击事件处理方法
            public void doClear(View view) {
                etName.setText("");
                rbMale.setChecked(true);
                cbMusic.setChecked(false);
                cbRead.setChecked(false);
                cbFood.setChecked(false);
                tvResult.setText("");

            }

            //    退出按钮单击事件处理方法
            public void doExit(View view) {
                finish();
            }
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值