Radio buttons and checkboxes pass data

Table of contents

theory:

radio button group

common attributes

Set up event listeners

single button

Set up event listeners

check box

common method

Set up event listeners

 Concrete example:

Create an Android application

 Copy the background image to the drawable directory

Open the string resource file strings.xml and enter the code

Open the main layout resource file activity_main.xml and enter the code:

The final running effect:


theory:

In Android applications, the user is often required to choose from several options. Sometimes it is required to select only one, then a radio button (RadioButton) must be used, and sometimes the user is required to select multiple options, then a check box (CheckBox) must be used. .

radio button group

common attributes

Attributes meaning
orientation vertical or horizontal, determines whether the radio buttons are arranged vertically or horizontally
layout_width width (unit: dp)
layout_height Height (unit: dp)

Set up event listeners

set listener effect
setOnCheckedChangeListener Listen for changes in the selected state of the radio button
setOnClickListener Listen to whether the radio button group is clicked

single button

common method

method effect
isChecked() true or false, shows the checked state of the radio button
setChecked() The parameter is true or false, used to set the selected state of the radio button

Set up event listeners

set listener effect
setOnCheckedChangeListener Listen for changes in the selected state of the radio button
setOnClickListener Listen to whether the radio button is clicked

check box

common method

method effect
isChecked() true or false, shows the checked state of the checkbox
setChecked() The parameter is true or false, which is used to set the selected state of the check box

Set up event listeners

set listener effect
setOnCheckedChangeListener Listen for changes in the checked state of the checkbox
setOnClickListener Listen to whether the checkbox is clicked

Inheritance diagram of three controls

 Concrete example:

Create an Android application

Create an Android applicationSetBasicInformation

 Copy the background image to drawablethe directory

Open the string resource file strings.xml and enter the code

 Specific code:

<resources>
    <string name="app_name">Set basic information</string>
    <string name="set_information">Set basic information</string>
    <string name="name">姓名:</string>
    <string name="input_name">Please enter your name</string>
    <string name="gender">性别:</string>
    <string name="male">男</string>
    <string name="female">女</string>
    <string name="hobby">爱好:</string>
    <string name="music">音乐</string>
    <string name="read">阅读</string>
    <string name="food">美食</string>
    <string name="ok">确定</string>
    <string name="clear">清除</string>
    <string name="exit">退出</string>
</resources>

Open the main layout resource file activity_main.xmland enter the code:

<?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"/>


</LinearLayout>

Open the main interface classMainActivity输入代码:

 Specific code:

package net.zyt.set_basic;

import androidx.appcompat.app.AppCompatActivity;

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

import java.util.Locale;

public class MainActivity extends AppCompatActivity {
    private EditText etName;//Name edit box
    private RadioGroup rgGender;//Gender radio button
    private RadioButton rbMale;//Male radio button
    private RadioButton rbFemale;//female radio button
    private CheckBox cbMusic;//music check box
    private CheckBox cbRead;//read the check box
    private CheckBox cbFood;//food check box
    private TextView tvResult;//end tag

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set up the user interface using the layout resource file
        setContentView(R.layout.activity_main);
        // Get the control instance through the resource identifier
        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);

// //Register the click event handler method for the button
//        rgGender.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                switch (rgGender.getCheckedRadioButtonId()) {
// case R.id.rb_male://Select male radio button
// Toast.makeText(MainActivity.this, "You chose 【Male】!", Toast.LENGTH_SHORT);
//                        break;
// case R.id.rb_female://Select female radio button
// Toast.makeText(MainActivity.this, "You chose 【Female】!", Toast.LENGTH_SHORT);
//                        break;
//                }
//            }
        }


//Submit button click event handler method
            public void doOK(View view) {
                // get the name
                String name = etName.getText().toString().trim();
                // get gender
                String gender = "";
                //Determine which radio button is selected by the user
                switch (rgGender.getCheckedRadioButtonId()) {// judge according to the selected radio button id
                    case R.id.rb_male://Select male radio button
                        gender = rbMale.getText().toString();

                        break;
                    case R.id.rb_female:
                        gender = rbFemale.getText().toString();
                        break;
                }
                // get hobbies
                StringBuilder builder = new StringBuilder();//String Builder
                //Determine whether the user has selected the music checkbox
                if (cbMusic.isChecked()) {
                    builder.append(cbMusic.getText().toString() + "");
                }
                //Determine whether the user has selected the read checkbox
                if (cbRead.isChecked()) {
                    builder.append(cbRead.getText().toString() + "");
                }
                //Determine whether the user has selected the gourmet checkbox
                if (cbFood.isChecked()) {
                    builder.append(cbFood.getText().toString() + "");
                }
                String hobbies = builder.toString().trim();//remove spaces
                //Display basic information through labels
                String result = "姓名:" + name + "\n"
                        + "gender" + "gender" + "\n"
                        + "hobbies" + hobbies;
                tvResult.setText(result);

            }


            // Clear button click event handler
            public void doClear(View view) {
                etName.setText("");
                rbMale.setChecked(true);
                cbMusic.setChecked(false);
                cbRead.setChecked(false);
                cbFood.setChecked(false);
                tvResult.setText("");

            }

            // Exit button click event handler method
            public void doExit(View view) {
                finish();
            }
        }

The final running effect:

 

Guess you like

Origin blog.csdn.net/hollow_future/article/details/127924605