前言
大家好!無論您的年齡,只要您曾經使用過網路,您很可能都有填寫過問卷或者見過親朋好友參與問卷調查的情形。問卷調查是一種廣泛使用的方式,用來收集人們的意見、看法和經驗,並從中獲取有價值的信息。
在今天的課程中,我們將探討如何製作一份簡單而有效的CheckBox型式問卷。CheckBox問卷是一種常見的調查形式,它允許參與者在多個選項中做出多選擇。這種問卷形式尤其適用於收集參與者對於某個主題的多樣觀點,讓我們更全面地了解人們的看法。
在接下來的內容中,我們將介紹如何設計和建立這樣一份CheckBox型式的問卷。
讓我們一同開始學習,探索如何以創造性和實用性相結合的方式,打造出引人注目的CheckBox問卷吧!
功能
如同畫面所示,當被checkBox被選擇後送出跳出資料的結果。框線的部分上一張講過了就不多做介紹了
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="409dp"
        android:layout_height="596dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >
        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:gravity="center"
            android:textColor="@color/black"
            android:textSize="20sp"
            android:background="@drawable/borderline"
            android:text="喜好調查表" />
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/borderline"
                android:orientation="horizontal">
                <CheckBox
                    android:id="@+id/checkBox1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="港式料理"
                    android:textColor="@color/black"
                    android:textSize="20sp" />
                <CheckBox
                    android:id="@+id/checkBox2"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="美式料理"
                    android:textSize="20sp" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/borderline"
                android:orientation="horizontal">
                <CheckBox
                    android:id="@+id/checkBox3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="義式料理"
                    android:textSize="20sp" />
                <CheckBox
                    android:id="@+id/checkBox4"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="中式料理"
                    android:textSize="20sp" />
            </LinearLayout>
        </RadioGroup>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="6.5"
            android:orientation="vertical">
            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/borderline"
                android:gravity="center"
                android:text="Context"
                android:textSize="25sp" />
            <TextView
                android:id="@+id/textView5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/borderline"
                android:gravity="center"
                android:text="Context"
                android:textSize="25sp" />
            <TextView
                android:id="@+id/textView6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/borderline"
                android:gravity="center"
                android:text="Context"
                android:textSize="25sp" />
            <TextView
                android:id="@+id/textView7"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/borderline"
                android:gravity="center"
                android:text="Context"
                android:textSize="25sp" />
        </LinearLayout>
        <Button
            android:id="@+id/button4"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@drawable/borderline"
            android:text="送出" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Java code
package com.example.checkbox_question;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    private CheckBox CheckBox1;
    private CheckBox checkBox2;
    private CheckBox checkBox3;
    private CheckBox checkBox4;
    private TextView context_textview;
    private TextView context_textview2;
    private TextView context_textview3;
    private TextView context_textview4;
    private Button sent_button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ById();//綁定元件
        SentButtonListener();//Sent_Button的監聽器 Listener
    }
    //綁定元件
    public void ById(){
        context_textview = findViewById(R.id.textView);
        context_textview2 = findViewById(R.id.textView5);
        context_textview3 = findViewById(R.id.textView6);
        context_textview4 = findViewById(R.id.textView7);
        CheckBox1 = findViewById(R.id.checkBox1);
        checkBox2 = findViewById(R.id.checkBox2);
        checkBox3 = findViewById(R.id.checkBox3);
        checkBox4 = findViewById(R.id.checkBox4);
        sent_button = findViewById(R.id.button4);
    }
    //Sent_Button的監聽器 Listener
    public void SentButtonListener(){
        sent_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mySelect_onCheckbox();//用來判別CheckBox是否被勾選與設定TextView
            }
        });
    }
    //用來判別CheckBox是否被勾選與設定TextView
    public void mySelect_onCheckbox(){
        //checkBox1 ~ 4分別為 港式 美式 義式 中式
        if(CheckBox1.isChecked()){
            context_textview.setText("港式料理");
        }
        if(checkBox2.isChecked()){
            context_textview2.setText("美式料理");
        }
        if(checkBox3.isChecked()){
            context_textview3.setText("義式料理");
        }
        if(checkBox4.isChecked()){
            context_textview4.setText("中式料理");
        }
    }
}
結言
注意(Notice):CheckBox1.isChecked()是boolean值代表是否被選擇,可以用它來判斷是否勾選。
那這次教學就到這邊,謝謝各位大大們