Learning "Android Studio Development Practice" (5) - Questionnaire


background

Here continue to learn how to use Android Studio 4.1.3 1 , learn the use of CheckBox, switch button Switch and radio button RadioButton.

Design of layout files

Now I want to design a single-page button display tool, including 4 check boxes 2, 4 switch buttons, 2 radio buttons, and 1 button. The top-to-bottom and left-to-right layouts are

  1. Title, with a width of 100% and a height of 10%, is used to display the title text "Questionnaire";
  2. Check boxes, each with a width of 50%, a height of 10%, two rows and two columns, the content is a survey of personal hobbies, including four items of eating, sleeping, singing, and sports, and a title "Hobbies";
  3. Switch buttons, each with a width of 50% and a height of 10%, with two rows and two columns, the content is mobile phone settings, including Bluetooth, location, mobile data, personal hotspot, and a title "Phone Settings";
  4. Radio buttons, each with a width of 50% and a height of 10%, with one row and two columns, the content is favorite singers, including Jackson and Madonna, and a title "Singer".
  5. The display button has a width of 100% and a height of 10%. After clicking, a message box will pop up, displaying personal hobbies, switch status, and personal marital status.

Writing layout files

activity_main.xmlThe code is as follows:

<?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">
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline_1"
        app:layout_constraintGuide_percent=".10"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline_2"
        app:layout_constraintGuide_percent=".20"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline_3"
        app:layout_constraintGuide_percent=".30"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline_4"
        app:layout_constraintGuide_percent=".40"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline_5"
        app:layout_constraintGuide_percent=".50"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline_6"
        app:layout_constraintGuide_percent=".60"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline_7"
        app:layout_constraintGuide_percent=".70"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline_8"
        app:layout_constraintGuide_percent=".80"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline_9"
        app:layout_constraintGuide_percent=".90"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline_10"
        app:layout_constraintGuide_percent=".50"
        android:orientation="vertical"/>
    <TextView
        android:id="@+id/tv_head"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:background="#90ee90"
        android:text="调查问卷"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_1" />
    <TextView
        android:id="@+id/tv_1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="你的爱好有哪些?"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_1"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_2" />
    <CheckBox
        android:id="@+id/cb_eat"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="吃饭"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="@+id/guideline_10"
        app:layout_constraintTop_toTopOf="@+id/guideline_2"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_3" />
    <CheckBox
        android:id="@+id/cb_sleep"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="睡觉"
        app:layout_constraintLeft_toLeftOf="@+id/guideline_10"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_2"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_3" />
    <CheckBox
        android:id="@+id/cb_sing"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="唱歌"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="@+id/guideline_10"
        app:layout_constraintTop_toTopOf="@+id/guideline_3"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_4" />
    <CheckBox
        android:id="@+id/cb_run"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="跑步"
        app:layout_constraintLeft_toLeftOf="@+id/guideline_10"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_3"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_4" />
    <TextView
        android:id="@+id/tv_2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="你会怎样设置你的手机?"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_4"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_5" />
    <Switch
        android:id="@+id/sw_1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="蓝牙:"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="@+id/guideline_10"
        app:layout_constraintTop_toTopOf="@+id/guideline_5"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_6" />
    <Switch
        android:id="@+id/sw_2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="位置信息:"
        app:layout_constraintLeft_toLeftOf="@+id/guideline_10"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_5"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_6" />
    <Switch
        android:id="@+id/sw_3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="移动数据:"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="@+id/guideline_10"
        app:layout_constraintTop_toTopOf="@+id/guideline_6"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_7" />
    <Switch
        android:id="@+id/sw_4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="个人热点:"
        app:layout_constraintLeft_toLeftOf="@+id/guideline_10"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_6"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_7" />
    <TextView
        android:id="@+id/tv_3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center"
        android:text="你喜欢哪个歌手?"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_7"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_8" />
    <RadioGroup
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_8"
        app:layout_constraintBottom_toBottomOf="@+id/guideline_9" >
        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="210dp"
            android:layout_height="70dp"
            android:text="迈克尔·杰克逊"
            app:layout_constraintBottom_toBottomOf="@+id/guideline_9"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="@+id/guideline_10"
            app:layout_constraintTop_toTopOf="@+id/guideline_8" />
        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="195dp"
            android:layout_height="70dp"
            android:text="麦当娜"
            app:layout_constraintBottom_toBottomOf="@+id/guideline_9"
            app:layout_constraintLeft_toLeftOf="@+id/guideline_10"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline_8" />
    </RadioGroup>
    <Button
        android:id="@+id/btn_show"
        android:layout_width="120dp"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:text="显示结果"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline_9" />
</androidx.constraintlayout.widget.ConstraintLayout>

10 auxiliary lines are used, as shown in the figure below:
Learning "Android Studio Development Practice" (5) - Questionnaire - Page Layout
It is worth noting that although RadioGroup has set 4 constraint_layout values, the RadioButton also needs to set layout_width and layout_height to display normally.

Writing code files

MainActivity.javaThe code is as follows:

package com.example.questionaire;

import androidx.appcompat.app.AppCompatActivity;
import android.widget.*;
import android.view.View;
import android.view.View.*;
import android.os.Bundle;
import java.util.*;

public class MainActivity extends AppCompatActivity {
    
    
    private Button btn_show;
    private CheckBox cb_eat, cb_sing, cb_sleep, cb_run;
    private Switch sw_1, sw_2, sw_3, sw_4;
    private RadioButton rb_1, rb_2;
    private Map<String, String> H1 = new HashMap<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_show = findViewById(R.id.btn_show);
        cb_eat = findViewById(R.id.cb_eat);
        cb_sleep = findViewById(R.id.cb_sleep);
        cb_sing = findViewById(R.id.cb_sing);
        cb_run = findViewById(R.id.cb_run);
        sw_1 = findViewById(R.id.sw_1);
        sw_2 = findViewById(R.id.sw_2);
        sw_3 = findViewById(R.id.sw_3);
        sw_4 = findViewById(R.id.sw_4);
        rb_1 = findViewById(R.id.rb_1);
        rb_2 = findViewById(R.id.rb_2);

        cb_eat.setOnCheckedChangeListener(new ClickTAction());
        cb_sleep.setOnCheckedChangeListener(new ClickTAction());
        cb_sing.setOnCheckedChangeListener(new ClickTAction());
        cb_run.setOnCheckedChangeListener(new ClickTAction());
        sw_1.setOnCheckedChangeListener(new ClickTAction());
        sw_2.setOnCheckedChangeListener(new ClickTAction());
        sw_3.setOnCheckedChangeListener(new ClickTAction());
        sw_4.setOnCheckedChangeListener(new ClickTAction());
        rb_1.setOnCheckedChangeListener(new ClickTAction());
        rb_2.setOnCheckedChangeListener(new ClickTAction());
        btn_show.setOnClickListener(new ClickCAction());

        H1.put("蓝牙", "close");
        H1.put("位置信息", "close");
        H1.put("移动数据", "close");
        H1.put("个人热点", "close");
        H1.put("radioButton", "");
    }
    private class ClickCAction implements OnClickListener {
    
    
        @Override
        public void onClick(View v) {
    
    
            if (v.getId() == R.id.btn_show) {
    
    
                String r = "你的爱好有:";
                Set<String> keys = H1.keySet();
                for(String k : keys) {
    
    
                    if (!k.equals("radioButton") && !k.equals("蓝牙") && !k.equals("位置信息") && !k.equals("移动数据") && !k.equals("个人热点")) {
    
    
                        r += H1.get(k) + ", ";
                    }
                }
                r = r.substring(0, r.length()-2);
                r += "\n你喜欢把手机设置成:蓝牙" + H1.get("蓝牙") + ", 位置信息" + H1.get("位置信息") + ", 移动数据" + H1.get("移动数据") + ", 个人热点" + H1.get("个人热点");
                r += "\n你喜欢的歌手是:" + H1.get("radioButton");
                Toast.makeText(MainActivity.this, r, Toast.LENGTH_SHORT).show();
            }
        }
    }
    private class ClickTAction implements CompoundButton.OnCheckedChangeListener {
    
    
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
    
            if (buttonView.getId() == R.id.cb_eat) {
    
    
                if (isChecked) {
    
    
                    H1.put("eat", "吃饭");
                } else {
    
    
                    H1.remove("eat");
                }
            }
            if (buttonView.getId() == R.id.cb_sing) {
    
    
                if (isChecked) {
    
    
                    H1.put("sing", "唱歌");
                } else {
    
    
                    H1.remove("sing");
                }
            }
            if (buttonView.getId() == R.id.cb_sleep) {
    
    
                if (isChecked) {
    
    
                    H1.put("sleep", "睡觉");
                } else {
    
    
                    H1.remove("sleep");
                }
            }
            if (buttonView.getId() == R.id.cb_run) {
    
    
                if (isChecked) {
    
    
                    H1.put("run", "跑步");
                } else {
    
    
                    H1.remove("run");
                }
            }
            if (buttonView.getId() == R.id.sw_1) {
    
    
                if (isChecked) {
    
    
                    H1.put("蓝牙", "open");
                } else {
    
    
                    H1.put("蓝牙", "close");
                }
            }
            if (buttonView.getId() == R.id.sw_2) {
    
    
                if (isChecked) {
    
    
                    H1.put("位置信息", "open");
                } else {
    
    
                    H1.put("位置信息", "close");
                }
            }
            if (buttonView.getId() == R.id.sw_3) {
    
    
                if (isChecked) {
    
    
                    H1.put("移动数据", "open");
                } else {
    
    
                    H1.put("移动数据", "close");
                }
            }
            if (buttonView.getId() == R.id.sw_4) {
    
    
                if (isChecked) {
    
    
                    H1.put("个人热点", "open");
                } else {
    
    
                    H1.put("个人热点", "close");
                }
            }
            if (buttonView.getId() == R.id.rb_1) {
    
    
                if (isChecked) {
    
    
                    H1.put("radioButton", "迈克尔·杰克逊");
                }
            }
            if (buttonView.getId() == R.id.rb_2) {
    
    
                if (isChecked) {
    
    
                    H1.put("radioButton", "麦当娜");
                }
            }
        }
    }
}

The use of check box CheckBox, switch button Switch and radio button RadioButton

Check box CheckBox, switch button Switch and radio button RadioButton all belong to the abstract class CompoundButton1. The main attributes used here are id (specify the name of the control), layout_height, layout_width (the height and width of the control), text (the text next to the check box, switch button, and radio button). The layout of radio buttons is slightly different, because to select one item in a group of buttons, a container RadioGroup is needed to determine the range of the group of buttons [1]. These three buttons need to use setOnCheckedChangeListener to register the check listener in the code, and the corresponding interface is CompoundButton.OnCheckedChangeListener.

cb_eat.setOnCheckedChangeListener(new ClickTAction());

Running Results
According to the previously explored method 2 to generate the apk file, and then transfer it to the mobile phone to run, the results are as follows:
Learning "Android Studio Development Practice" (5) - Questionnaire - Running Results


  1. Ouyang Shen. Android Studio Development Practice. Tsinghua University Press. 2017. ↩︎

  2. Learning "Android Studio Development Practice" (1) - Hello World_Xiatangren's Blog-CSDN Blog_android studio learning program development↩︎

Guess you like

Origin blog.csdn.net/quanet_033/article/details/128274862