今天我们继续延续上篇博客内容,教大家复选框制作,这里我们为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>