[Android Studio 30天自我挑战] Radiobutton和Checkbox的练习

上一篇讲到Radiobutton为单选,需要复选时就可以使用Checkbox
这篇我们利用Radiobutton和Checkbox
来制作选择性别(单选)以及选择兴趣(复选)的APP!

xml档如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="性别:"
        android:textSize="30dp" />
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="男生"
            android:textSize="30dp" />
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="女生"
            android:textSize="30dp" />
    </RadioGroup>
    <TextView
        android:id="@+id/tx"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="你的兴趣:"
        android:textSize="30dp" />
    <CheckBox
        android:id="@+id/ch1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="篮球" />
    <CheckBox
        android:id="@+id/ch2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="打排球" />
    <CheckBox
        android:id="@+id/ch3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="游泳" />
    <CheckBox
        android:id="@+id/ch4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30dp"
        android:text="跑步" />
    <Button
        android:id="@+id/okbutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="送出"
        android:textSize="30dp" />
</LinearLayout>

MainActivity的范例如下:

package com.example.itradiobutton;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private CheckBox ch1,ch2,ch3,ch4;
    private RadioGroup radioGroup;
    private RadioButton radioButton;
    private Button okbutton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ch1 = findViewById(R.id.ch1);
        ch2 = findViewById(R.id.ch2);
        ch3 = findViewById(R.id.ch3);
        ch4 = findViewById(R.id.ch4);
        radioGroup = findViewById(R.id.radioGroup);
        okbutton = findViewById(R.id.okbutton);
        //设定按下Button之後
        okbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer result = new StringBuffer();

                int gender = radioGroup.getCheckedRadioButtonId();
                radioButton = findViewById(gender);
                if (radioButton != null)
                {
                    result.append("\n性别是:\n").append(radioButton.getText().toString()).append("\n");
                }

                result.append("兴趣是:\n");
                if (ch1.isChecked()){
                    result.append(ch1.getText().toString()).append("\n");
                }
                if (ch2.isChecked()){
                    result.append(ch2.getText().toString()).append("\n");
                }
                if (ch3.isChecked()){
                    result.append(ch3.getText().toString()).append("\n");
                }
                if (ch4.isChecked()){
                    result.append(ch4.getText().toString()).append("\n");
                }
                Toast.makeText(MainActivity.this,result.toString(),Toast.LENGTH_LONG).show();
            }
        });
    }
}

这样就完成罗
成果如下:
https://ithelp.ithome.com.tw/upload/images/20211002/201392588ckAWgox1h.png
https://ithelp.ithome.com.tw/upload/images/20211002/20139258cbYZCX6YAI.png


<<:  18 在 Phoenix 1.6 安装 tailwindcss

>>:  Day32 ( 游戏设计 ) 太空狗闪躲陨石

DAY21 - 金鱼脑学了新的忘了旧的

前言 今天是铁人赛的第二十一天,终於把之前超前部署的文章存档的扣打都用完了 今天上传的文章,今天才开...

[Day 6] Course 2_Ask Questions - 有效的提问并做资料导向决策

《30天带你上完 Google Data Analytics Certificate 课程》系列将...

剪裁与遮罩-30天学会HTML+CSS,制作精美网站

有时候在制作区块时,会希望用不规则的形状呈现,以前会将图片制作成不规则形状,在放到html里面,或是...

DAY 11 Operators

Operators 有时候画面的比例,会影响 CSS 显示的效果,而在 SASS 中,我们可以用数学...

Day29 - Exploitation- Linux kernels 漏洞

Linux kernels 常有一些可以从一般使用者提权到 root 的漏洞,如 DirtyCOW ...