Android常见界面控件(二)

news2024/9/22 11:34:05

目录

前言

一、 RadioButton控件

设置RadioGroup的监听事件

二、CheckBox控件

三、Toast类

改变Toast窗口的提示位置


前言

在上一篇中,我们讲解了三个常见的控件:TextView控件、Button控件、ImageView控件,那么本篇我们就接着讲剩下的一些常见的界面控件。

一、 RadioButton控件

RadioButton控件是单选控件,是Button的子类。每一个按钮都有“选中”和“未选中”两种状态。我们可以通过android:checked属性来指定是否选择,true为选中,false为未选中。

RadioButton控件一般搭配RadioGroup使用RadioGroup是单选复合框,能够容纳多个RadioButton控件,在RadioGroup中不会存在多个RadioButton同时选中的情况。在XML布局文件中的格式:

 <RadioGroup
        android:属性名="属性值"
        .....>
        <RadioButton
         android:属性名="属性值"
                     ....../>
    </RadioGroup>

注意:由于RadioGroup继承于LinearLayoout,所以我们可以指定水平还是竖直布局

设置RadioGroup的监听事件

我们在设置完XML布局文件之后,可以在java类中来设置RadioGroup的监听事件,哪个RadioButton被点击,就处理被点击控件的点击事件。

通过调用setOnCheckChangeListener()法为RadioGroup来设置监听布局内的控件状态是否改变,同时需要重写方法中的onCheackChanged方法,在该方法中来实现我们的功能。

示例:

我们来实现一个选择性别的单选界面,并且当哪个按钮被点击后,要在下方显示用户选择的性别。

XML布局如下:

<?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:orientation="vertical">

    <RadioGroup
        android:id="@+id/rg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id="@+id/male"
            android:text="男"
            android:textSize="40sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <RadioButton
            android:id="@+id/female"
            android:text="女"
            android:textSize="40sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </RadioGroup>
    <TextView
        android:id="@+id/result"
        android:text="您的性别是:"
        android:textSize="30sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

定义一个Demo1类

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

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class Demo1 extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private TextView textView;
    private RadioGroup rg;

   @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.radio);
    textView = findViewById(R.id.result);
    if (textView == null) {
        throw new IllegalStateException("Result TextView not found.");
    }
    rg = findViewById(R.id.rg);
    if (rg == null) {
        throw new IllegalStateException("RadioGroup not found.");
    }
    rg.setOnCheckedChangeListener(this);
}

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        if (i==R.id.male){
            textView.setText("您的性别是:男");
        }else if (i==R.id.female){
            textView.setText("您的性别是:女");
        }
    }
}

运行结果如下:

 

二、CheckBox控件

CheckBox控件表示复选框,是Button的子类,用于实现多选功能。与单选类似,也有两种状态,也是通过android:checked属性来指定状态

示例:通过CheckBox来统计用户的兴趣爱好。

 设置一个名为check_box的XML布局文件

<?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:orientation="vertical">

    <TextView
        android:text="请选择你的兴趣爱好:"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/ball"
        android:text="篮球"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/ping"
        android:text="乒乓球"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <CheckBox
        android:id="@+id/shuttle"
        android:text="羽毛球"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:text="您的爱好为:"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/result"
        android:textSize="20sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

设置一个名为CheckActivity的类。

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

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * CheckActivity 类继承自 AppCompatActivity,并实现了 CompoundButton.OnCheckedChangeListener 接口,
 * 用于处理 CheckBox 的选中状态变化事件。
 */
public class CheckActivity  extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    // 定义一个字符串变量,用于存储用户选择的爱好
    private String hobbys;
    // 定义一个 TextView 变量,用于显示用户选择的爱好
    private TextView hobby;

    /**
     * 在 onCreate 方法中设置布局和初始化 CheckBox 和 TextView,
     * 并为每个 CheckBox 设置监听器,以便在选中状态变化时调用 onCheckedChanged 方法。
     */
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cheack_box);
        CheckBox ball=findViewById(R.id.ball);
        CheckBox ping=findViewById(R.id.ping);
        CheckBox shuttle=findViewById(R.id.shuttle);
        ball.setOnCheckedChangeListener(this);
        ping.setOnCheckedChangeListener(this);
        shuttle.setOnCheckedChangeListener(this);
        hobby = findViewById(R.id.result);
        hobbys =new String();
    }

    /**
     * 当 CompoundButton 的选中状态被改变时,这个方法被调用。
     * 
     * @param compoundButton 被改变的 CompoundButton 对象
     * @param b              新的选中状态,true 表示选中,false 表示未选中
     */
    @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);
            }
        }
    }
}

当我们将这个类注册之后并运行,就可以得到:

 

三、Toast类

Toast类是android中提供的一个轻量级信息提醒机制,用于向用户提示即时信息,显示在应用程序界面的最上层,显示一段时间之后就会自动消失不会打断当前操作,也不获得焦点。

想要向用户使用Toast类提示即时信息需要调用其中的makeText()方法设置即时信息,再调用show()方法将提示信息显示到界面上。

Toast.makeText(Context,Text.Time).show();

 我们来解释一下上面几个参数的含义:

  • Context:表示应用程序环境的信息,即当前组件的上下文环境。Context是一个抽象类,如果在Activity中使用Toast类的提示信息,那么该参数可以设置为为“Activity.this”。
  • Text:表示提示的字符串信息。
  • Time:表示显示信息的时长。有两个属性值:
参数名含义
LENGTH_SHORT0Toast类显示较短的时间后消失(4000ms)
LENGTH_LONG1Toast类显示较长的时间后消失(7000ms)

示例:

        Toast.makeText(this,"请选择你的爱好",Toast.LENGTH_LONG).show();

改变Toast窗口的提示位置

如果我们想要改变提示的位置,我们可以使用Toast类中的setGravity()方法。

public void setGravity(int gravity, int xOffset, int yOffset) {
throw new RuntimeException(“Stub!”);
}
  • gravity:表示具体的位置。可以使用Gravity.CENTER、Gravity.TOP、Gravity.left表示
  • xoffset:表示移动的水平位置,若想向右移动,大于0即可。如果是0则不移动。
  • yoffset:表示移动的竖直位置。想向下移动,增大参数值即可。

示例:通过三个按钮来显示设置提醒的位置。

设置toast.xml界面布局

<?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:orientation="vertical">

    <Button
        android:id="@+id/defaultt"
        android:text="点击显示默认位置的位置"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/top"
        android:text="点击显示居中上部的位置"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/center"
        android:text="点击显示居中的位置"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

设置Demo2.java类

package com.example.newapptext1;

import static android.widget.Toast.LENGTH_LONG;
import static android.widget.Toast.LENGTH_SHORT;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class Dmo2 extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toast);
       findViewById(R.id.defaultt).setOnClickListener(this);
       findViewById(R.id.top).setOnClickListener(this);
       findViewById(R.id.center).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Toast toast;
        if(view.getId()==R.id.defaultt){
            toast = Toast.makeText(this,"默认",LENGTH_SHORT);
            toast.show();
        }else if(view.getId()==R.id.top){
            toast = Toast.makeText(this,"顶部",LENGTH_SHORT);
            toast.setGravity(Gravity.TOP,0,0);
            toast.show();
        }else if (view.getId()==R.id.center){
            toast = Toast.makeText(this,"居中",LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
        }
    }
}

运行结果如下: 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2055487.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Selenium实战:深度解析Python中嵌套Frame与iFrame的定位与切换技巧,解决Selenium定位不到的问题

在Web自动化测试中&#xff0c;处理网页中的Frame和iFrame是常见的挑战之一。这些元素在网页中扮演着承载独立HTML文档的角色&#xff0c;使得直接定位或操作其中的元素变得复杂。Python的Selenium库提供了强大的工具来应对这些挑战&#xff0c;本文将详细介绍如何使用Selenium…

SFP光模块、gt口、PMD、PMA、PCS之间的关系

ZYNQ内部的GT&#xff08;高速收发器&#xff09;接口包含了PCS&#xff08;物理编码子层&#xff09;与PMA&#xff08;物理介质接入层&#xff09;。这两个层在高速数据传输中起着至关重要的作用。 PCS层&#xff08;物理编码子层&#xff09; PCS层位于协调子层&#xff0…

Ubuntu虚拟机服务器的搭建

01.VMware安装 略。 02.Ubuntu虚拟机安装 略。 03.配置Ubuntu虚拟机网络 参考视频&#xff1a; Ubutu虚拟机网络配置&#xff08;桥接&#xff09;https://www.bilibili.com/video/BV1bG411V72A/?spm_id_from333.999.0.0&vd_sourced1fd4bcc46805ab35cc8bbb5a8bf318f…

win11如何查看串口的名字

1、右击win然后点击设备管理器 2、点击端口然后右击串口点击属性 3、进入窗口后点击Port information即可看见Port name属性就是串口名字

uniapp/vue如何实现一个子表单及子表单作用

子表单是一个辅助表单或一个表&#xff0c;它允许在主表单中添加多个行式项目&#xff0c;以处理与主记录相关联的多个辅助项目或数据。子表单在多种应用场景中发挥着重要作用&#xff0c;特别是在需要处理一对多关系的数据时。 以下是对子表单的详细解析&#xff1a; 定义与特…

苍穹外卖day10

苍穹外卖day10 Spring Task订单状态定时处理WebSocket应用&#xff08;弹幕&#xff0c;网页聊天&#xff0c;体育实况更新&#xff0c;股票基金实时更新&#xff09; 来单题型代码实现需求分析 客户催单 Spring Task 链接: 在线生成器 在线生成器 订单状态定时处理 每分钟检…

木舟0基础学习Java的第二十五天(JavaWeb)

XML 概念和体系 XML指可扩展标记语言&#xff08;EXtensible Markup Language&#xff09; XML没有预定义标签 需要自定义标签 <标签></标签> XML特点 XML数据以纯文本格式存储 实现不同应用程序之间的数据通信 实现不同平台的数据通信 实现不同平台的数据共…

Linux------Cortex-A架构的处理器运行模型与其寄存器组

寄存器组分为 外设寄存器组 比如&#xff1a;和总线相连的io寄存器&#xff0c;usart配置寄存器&#xff0c;spi配置寄存器等等 内核寄存器组&#xff1a;R0-R15 CPSR SPSR一共18个寄存器组&#xff0c;内核寄存器组用来记录当前程序地址状态&#xff0c;当前执行指令等&a…

8月来得及|1000/660/880题45天带刷计划!

刷题不在于多&#xff0c;而在于精 你选的这几本题集没有问题&#xff0c;660题专门训练客观题&#xff0c;880题和1000题都是不错的综合性题集&#xff0c;特别适合在强化阶段进行刷题训练&#xff0c;但是问题是你做这么多题&#xff0c;也不一定能起到多好的作用。 刷题的…

Linux | 进程概念详解:冯诺依曼体系结构、进程基本概念、PCB、进程组织、fork()创建子进程、进程运行逻辑

文章目录 进程概念1、冯诺依曼体系结构2、进程2.1基本概念2.2描述进程-PCB2.3组织进程2.4查看进程2.5通过系统调用获取进程标识符2.6通过系统调用创建进程-fork初识头文件与返回值fork函数的调用逻辑和底层逻辑 进程概念 1、冯诺依曼体系结构 目前我们认识的计算机中&#xff…

dm 到 dm 的 HS 同步部署

一、数据库部署 完成两节点数据库部署并初始化&#xff0c;配置参数如下&#xff1a; 节点 192.168.2.132 192.168.2.133 数据库版本 DM8 DM8 实例名 DM1 DM2 端口号 5236 5236 数据文件路径 /home/dmdba/dmdata /home/dmdba/dmdata 二、 dmhs 部署 1. 部署…

户外无线麦克风哪个牌子好,领夹麦克风十大品牌排行榜推荐

​为了在数字时代的浪潮中有效传达自己的思想和感情&#xff0c;选择合适的声音工具变得尤为重要。无线领夹麦克风就是这样一种工具&#xff0c;它能够帮助我们在任何环境下都保持最佳的语音传递效果。经过慎重考虑和市场调研&#xff0c;我为大家推荐几款性能卓越且价格合理的…

测试报告----功能测试

目录 一、项目背景 二、项目功能 &#xff08;1&#xff09;页面1&#xff1a;用户注册 &#xff08;2&#xff09;页面2&#xff1a;登陆页面 &#xff08;3&#xff09;页面3&#xff1a;信息主页 &#xff08;4&#xff09;页面4&#xff1a;详情页面 ​编辑&#xf…

获取当前路由器的外网IP(WAN IP)

GPT-4o (OpenAI) 获取当前路由器的外网IP&#xff08;WAN IP&#xff09;可以通过以下几种方法&#xff1a; 1. 访问路由器管理页面&#xff1a; - 通常路由器的管理页面可以通过在浏览器中输入路由器的IP地址来访问&#xff08;例如&#xff0c;192.168.0.1 或 192.168.1…

接口基础知识9:详解response body(响应体)

课程大纲 一、定义 HTTP响应体&#xff08;HTTP Response Body&#xff09;&#xff1a;服务器返回给客户端的数据部分&#xff0c;‌它包含了服务器对客户端请求的响应内容&#xff08;如客户端请求的资源、客户端请求的执行结果&#xff09;。 与请求类似&#xff0c;HTTP …

使用python实现3D聚类图

实验记录&#xff0c;在做XX得分预测的实验中&#xff0c;做了一个基于Python的3D聚类图&#xff0c;水平有限&#xff0c;仅供参考。 一、以实现三个类别聚类为例 代码&#xff1a; import pandas as pd import numpy as np from sklearn.decomposition import PCA from sk…

【Prometheus】监控系统

目录 一.Prometheus概述 1.Prometheus的认识与了解 2.Prometheus的特点 3.Prometheus存储引擎 TSDB 的特点优势 4.Prometheus 的生态组件 Prometheus server Client Library Exporters Service Discovery Alertmanager Pushgateway Grafana 5.Prometheus 的…

二叉树(四)

一、二叉树的性质 二、练习 1.某二叉树共有399个节点&#xff0c;其中有199个度为2的节点&#xff0c;则二叉树中的叶子节点数为&#xff08; &#xff09;。 A.不存在这样的树 B.200 C.198 D.199 答案&#xff1a;B 参考二叉树的性质第三条 2.在具有2…

如何将系统/数据/程序从一个硬盘驱动器迁移到另一个硬盘驱动器

何时需要硬盘迁移软件 大多数时候&#xff0c;计算机用户考虑迁移硬盘的原因是&#xff1a; 旧硬盘太小&#xff0c;无法存储不断增加的数据&#xff0c;您需要将这些数据转移到更大的硬盘上。 您购买了比当前硬盘更大更快的新硬盘&#xff0c;并且想要将程序和数据移动到新硬…

数据库分库分表的介绍

为什么要分库分表 把存于一个库的数据分散到多个库中&#xff0c;把存于一个表的数据分散到多个表中。如果说读写分离是为了分散数据库读写操作压力&#xff0c;分库分表就是为了分散存储压力&#xff0c;一般情况下&#xff0c;单表数据量到达千万级别&#xff0c;就可以考虑…