Android Studio开发学习(四、单选多选框)

        今天我们继续延续上篇博客内容,教大家复选框制作,这里我们为QueryActivity添加具体点击内容。这里介绍一下今天接触到的一个新布局ScrollView:

  ScrollView 是 Android 开发中一个非常有用的布局容器,它允许你滚动查看内容,即使这些内容的总高度或总宽度超过了屏幕的尺寸。ScrollView 通常只能在一个方向上滚动(通常是垂直方向),但如果你需要两个方向都滚动,可以使用 HorizontalScrollView(水平滚动)或者嵌套使用 ScrollView 和 HorizontalScrollView。

1. ScrollView

        这里我们就先简单演示一下滚动功能(我将他放在 activity_record 中演示,下篇文章再来修改增加其他功能,所以完整代码中就先不展示 activity_record 和 RecordActivity 部分)

2. 单选框

        在 Android 开发中,RadioGroup 和 RadioButton 是两个经常一起使用的 UI 组件,它们共同用于实现单选按钮的功能。这种机制允许用户从一组选项中选择一个,确保同一时间只有一个选项被选中。

        RadioGroup 是一个能够管理一组 RadioButton 的容器。它确保了同一时间只有一个RadioButton 能被选中。当你点击 RadioGroup 中的任何一个 RadioButton 时,RadioGroup会自动取消其他所有 RadioButton 的选中状态,并将新点击的 RadioButton 设置为选中状态。RadioGroup 提供了 check(int id) 和 clearCheck() 等方法,分别用于选择指定的RadioButton(通过其ID)和清除所有 RadioButton 的选中状态。但是,在大多数情况下,我们不需要直接调用这些方法,因为用户点击 RadioButton 时,RadioGroup 会自动处理这些逻辑。

         RadioButton 是一个允许用户从一组选项中选择一个的按钮。它本身并不直接实现单选逻辑,而是需要与 RadioGroup 配合使用来实现这一功能。每个 RadioButton 都可以设置一个唯一的ID,但这个ID在单选逻辑中并不是必需的,因为 RadioGroup 通过遍历其子视图来管理哪个 RadioButton 被选中。它继承自CompoundButton,因此它拥有 isChecked() 和setChecked(boolean checked)等方法,可以用来检查或设置按钮的选中状态。但是,在RadioGroup 环境中,通常会通过 RadioGroup 来管理这些状态,而不是直接操作RadioButton 。

         当我们进入单选界面,希望它本身具有默认选项时,将 android:checked="true" 语句添加到你指定的按钮下即可。

3. 多选框

        CheckBox允许用户通过勾选或取消勾选来表示一个选项的选中状态。与 RadioButton 不同的是,CheckBox 不需要被包含在一个 RadioGroup 中来实现单选逻辑,因为 CheckBox  本身就可以独立地表示其选中状态,而且一个界面中可以有多个 CheckBox 同时被选中。

        CheckBox 继承自 CompoundButton,它也拥有 isChecked() 和setChecked(boolean checked)等方法,用于检查或设置 CheckBox 的选中状态。此外,CheckBox  还支持监听其选中状态变化的事件,通过 setOnCheckedChangeListener 方法可以设置一个监听器,当 CheckBox 的选中状态改变时,会回调监听器中的 onCheckedChanged 方法。

4. 调用方法--QueryActivity

        在Android应用程序中处理单选按钮和复选框的选中状态变化,并通过Toast向用户显示相应的信息。我们先定义找到控件,然后为控件添加相应方法功能。

        在 onCreate 方法中,通过调用 setContentView(R.layout.activity_query) 加载与这个Activity相关联的布局文件(activity_query.xml)。然后,使用 findViewById 方法获取布局文件中定义的RadioGroup 和 CheckBox 的实例。(初始化)

实机预览

 

完整代码

QueryActivity.java

package com.example.login.Function;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.login.R;


public class QueryActivity extends AppCompatActivity {

    private RadioGroup mRG1;
    private CheckBox mCB1;
    private CheckBox mCB2;
    private CheckBox mCB3;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query);

        mRG1 = findViewById(R.id.RG_1);
        mCB1 = findViewById(R.id.CB_1);
        mCB2 = findViewById(R.id.CB_2);
        mCB3 = findViewById(R.id.CB_3);

        mRG1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = group.findViewById(checkedId);
                Toast.makeText(QueryActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });

        mCB1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(QueryActivity.this, isChecked?"选中":"未选中", Toast.LENGTH_SHORT).show();
            }
        });

        mCB2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(QueryActivity.this, isChecked?"选中":"未选中", Toast.LENGTH_SHORT).show();
            }
        });

        mCB3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(QueryActivity.this, isChecked?"选中":"未选中", Toast.LENGTH_SHORT).show();
            }
        });

    }
}

activity_query.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/background1"
    tools:context=".Function.QueryActivity">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:src="@drawable/jingliu"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="80dp"
            android:scaleType="centerCrop"/>

        <!--单选框-->
        <RadioGroup
            android:id="@+id/RG_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            android:layout_marginTop="100dp">

            <!-- 默认为这个选项——true-->
            <RadioButton
                android:id="@+id/RB_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="当前信息"
                android:textColor="@color/colorBlack"
                android:textSize="20sp"
                android:checked="true"/>
            <RadioButton
                android:id="@+id/RB_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="添加用户"
                android:textColor="@color/colorBlack"
                android:textSize="20sp"/>
            <RadioButton
                android:id="@+id/RB_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="查询信息"
                android:textColor="@color/colorBlack"
                android:textSize="20sp"/>
        </RadioGroup>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center_horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="添加用户"
                android:textColor="@color/colorBlack"
                android:layout_marginTop="50dp"
                android:textSize="20sp"/>

            <CheckBox
                android:id="@+id/CB_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="姓名"
                android:textColor="@color/colorBlack"
                android:layout_marginTop="20dp"/>
            <CheckBox
                android:id="@+id/CB_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="性别"
                android:textColor="@color/colorBlack"
                android:layout_marginTop="20dp"/>
            <CheckBox
                android:id="@+id/CB_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="出生年月日"
                android:textColor="@color/colorBlack"
                android:layout_marginTop="20dp"/>

        </LinearLayout>

    </LinearLayout>

</ScrollView>

  • 14
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,有两种常用的消息提示框可以使用:Toast提示框和AlertDialog。这两种提示框在功能和使用方式上有所不同。 1. Toast提示框是一种简单的消息提示框,用于向用户显示一条临时性的消息。它的代码示例如下: Toast.makeText(MainActivity.this, "有空输入!\n请重新输入!", Toast.LENGTH_SHORT).show(); 这段代码会在底部弹出一个短暂显示的提示框,其中包含有关输入错误的消息。Toast提示框适用于简单的文本提示,对于输入错误等情况较为合适。 2. AlertDialog是一种更为复杂和功能丰富的提示框,可以用于实现多种功能,例如提示说明、单选框、复选框甚至登录功能。AlertDialog可以通过编写XML文件来实现更多的功能和更好的界面。下面是一个使用AlertDialog的示例代码: Handler handler = new Handler(Looper.getMainLooper()); handler.post(new Runnable() { @Override public void run() { //放在UI线程弹AlertDialog的代码 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示") .setMessage("有空输入!\n请重新输入!") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //确定按钮的点击事件处理 } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //取消按钮的点击事件处理 } }) .show(); } }); 这段代码会创建一个AlertDialog并在UI线程中显示出来。其中,通过AlertDialog.Builder来构建对话框的内容,可以设置标题、消息内容以及确定和取消按钮的点击事件。通过调用show()方法来显示对话框。 总结来说,Toast提示框适用于简单的文本提示,而AlertDialog则可以实现更多的功能和更好的界面。具体选择哪种消息提示框取决于实际需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值