Android学习笔记篇2. 单选按钮复选按钮

Posted 嗯~你说今晚吃什么

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android学习笔记篇2. 单选按钮复选按钮相关的知识,希望对你有一定的参考价值。

单选按钮

在XML里写一组单选按钮(2个)+ 一个文本(用于提示):

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

    <RadioGroup
        android:id="@+id/rdg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id="@+id/rbtn"
            android:textSize="25dp"
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <RadioButton
            android:textSize="25dp"
            android:text=""
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RadioGroup>

    <TextView
        android:id="@+id/tv"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

在Java中编写代码

package com.example.no_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = (RadioGroup) findViewById(R.id.rdg);
        textView = (TextView) findViewById(R.id.tv);
        /**
         * 利用setOnCheckedChangeListtener()为RadioGroup设置监听事件
         */
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                // 判断被点击的RadioButton
                if (i == R.id.rbtn) {
                    textView.setText("您的性别是:男");
                } else {
                    textView.setText("您的性别是:女");
                }
            }
        });
    }
}

运行,点击按钮,在底下提示相应信息:

复选按钮

在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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请选择爱好:"
        android:textSize="18dp"/>

    <CheckBox
        android:id="@+id/shuttlecock"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="羽毛球"
        android:textSize="18dp"/>
    <CheckBox
        android:id="@+id/basketball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="篮球"
        android:textSize="18dp"/>

    <CheckBox
        android:id="@+id/pingpong"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="乒乓球"
        android:textSize="18dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="您选择的兴趣爱好为:"
        android:textSize="22dp"/>
    <TextView
        android:id="@+id/hobby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18dp"/>

</LinearLayout>

在Java中编写代码,利用TextView提示哪一个复选框被选择:

package com.example.no_2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private TextView hobby;
    private String hobbys;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /**
         * 初始化CheckBox控件
         */
        CheckBox shuttlecock = (CheckBox) findViewById(R.id.shuttlecock);
        CheckBox basketball = (CheckBox) findViewById(R.id.basketball);
        CheckBox pingpong = (CheckBox) findViewById(R.id.pingpong);
        shuttlecock.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        pingpong.setOnCheckedChangeListener(this);
        hobby = (TextView) findViewById(R.id.hobby);
        hobbys = new String();
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String motion = compoundButton.getText().toString();
        /**
         * 简易算法控制提示字符串
         */
        if (b) {
            if (!hobbys.contains(motion)) {
                hobbys = hobbys + motion;
                hobby.setText(hobbys);
            }
        } else {
            if (hobbys.contains(motion)) {
                hobbys = hobbys.replace(motion, "");
                hobby.setText(hobbys);
            }
        }
    }
}

运行,在底下提示对应信息:

以上是关于Android学习笔记篇2. 单选按钮复选按钮的主要内容,如果未能解决你的问题,请参考以下文章

Android学习笔记篇2. 单选按钮复选按钮

Android学习笔记篇2. 单选按钮复选按钮

安卓讲课笔记5.4 单选按钮和复选框

UI控件之RadioButton(单选按钮)&Checkbox(复选按钮)

Android-- 按钮(复选框CheckBox开关按钮Switch单选按钮RadioButton)

Android基础入门教程——2.3.5.RadioButton(单选按钮)&Checkbox(复选框)