Android——单选多选按钮的使用详解

article/2025/8/25 12:31:38

一:单选按钮

单选按钮类:RadioButton

android:checked="true"设置默认选中

单选按钮控件通常与RadioGroup搭配使用。 
     RadioGroup是LinearLayout的子类,用于将多个单选按钮组合为一组。 
     同一按钮组内的单选按钮只能有一个被选中。

二:多选按钮

用法基本与Button相同

CheckBox对象.isChecked()方法可以用来判断复选按钮是否选中 

效果图(单选多选写在一个项目里边,用了一个页面跳转):

项目目录:

多选按钮,两种形式

代码:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="${relativePackage}.${activityClass}" ><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="RadioActivity单选" /><Buttonandroid:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="CheckActivity多选" /></LinearLayout>

MainActivity.java

package com.example.radioandcheckdemo;import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class MainActivity extends Activity implements OnClickListener{private Button button1;private Button button2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);button1.setOnClickListener(this);button2.setOnClickListener(this);}@Overridepublic void onClick(View v) {Intent intent = new Intent();switch (v.getId()) {case R.id.button1://跳转页面intent.setClass(MainActivity.this, RadioActivity.class);startActivity(intent);break;case R.id.button2://跳转页面intent.setClass(MainActivity.this, CheckActivity.class);startActivity(intent);default:break;}}
}

activity_radio.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:layout_margin="20sp"tools:context="${relativePackage}.${activityClass}" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" /><!-- 单选android:checked="true"设置默认选中--><RadioGroupandroid:id="@+id/group1"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content" ><RadioButton android:id="@+id/radio1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="男"/><RadioButton android:id="@+id/radio2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"/></RadioGroup><!-- 分界线 --><Viewandroid:layout_width="match_parent"android:layout_height="2sp"android:background="@android:color/holo_blue_dark"android:layout_marginTop="10sp"android:layout_marginBottom="10sp" /><TextView android:id="@+id/text1"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="18sp"android:text="你吃饭了吗?"/><RadioGroupandroid:id="@+id/group2"android:layout_width="match_parent"android:layout_height="wrap_content" ><RadioButton android:id="@+id/radio3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="吃了"/><RadioButton android:id="@+id/radio4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="没吃"/></RadioGroup></LinearLayout>

RadioActivity.java

package com.example.radioandcheckdemo;import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;public class RadioActivity extends Activity implements OnCheckedChangeListener {private RadioGroup group1;private RadioGroup group2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_radio);group1 = (RadioGroup) findViewById(R.id.group1); group2 = (RadioGroup) findViewById(R.id.group2); group1.setOnCheckedChangeListener(this);group2.setOnCheckedChangeListener(this);}@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {//显示值的几种方法//checkedId选中RadioButton的id/*switch (checkedId) {case R.id.radio1:Toast.makeText(this, "男", Toast.LENGTH_LONG).show();break;case R.id.radio2:Toast.makeText(this, "女", Toast.LENGTH_LONG).show();break;case R.id.radio3:Toast.makeText(this, "吃了", Toast.LENGTH_LONG).show();break;case R.id.radio4:Toast.makeText(this, "没吃", Toast.LENGTH_LONG).show();break;default:break;}*///找到点击的RadioButton//RadioButton radio = (RadioButton) findViewById(checkedId);//取出RadioButton中的值//String str = radio.getText().toString();//弹框显示选中的值//Toast.makeText(this, str, Toast.LENGTH_LONG).show();//两组数据同时显示//根据RadioGroup取出数据,没有选中返回-1String str = "";int buttonId = group1.getCheckedRadioButtonId();if(buttonId != -1){RadioButton radio = (RadioButton) findViewById(buttonId);str = "你的性别是" + radio.getText().toString();			}else{str = "你没有选择性别";}buttonId = group2.getCheckedRadioButtonId();if(buttonId != -1){RadioButton radio = (RadioButton) findViewById(buttonId);str += ",   你吃饭了吗?"+radio.getText().toString();}Toast.makeText(this, str, Toast.LENGTH_LONG).show();}
}

activity_check.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="${relativePackage}.${activityClass}" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="选择所学课程:" /><CheckBoxandroid:id="@+id/check1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="HTML" /><CheckBoxandroid:id="@+id/check2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="C" /><CheckBoxandroid:id="@+id/check3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="php" /><CheckBoxandroid:id="@+id/check4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="java" /><Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="提交" /></LinearLayout>

CheckActivity.java

package com.example.radioandcheckdemo;import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;public class CheckActivity extends Activity {private CheckBox check1;private CheckBox check2;private CheckBox check3;private CheckBox check4;private Button button1;private OnCheckedChangeListener listenter = new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {//选中多选框CheckBox check = (CheckBox)buttonView;//取出当前勾选值String str = check.getText().toString();//判断是否勾选状态if(isChecked){str = "你学了"+str;}else{str = "你没学"+str;}Toast.makeText(CheckActivity.this, str, Toast.LENGTH_LONG).show();}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_check);check1 = (CheckBox) findViewById(R.id.check1);check2 = (CheckBox) findViewById(R.id.check2);check3 = (CheckBox) findViewById(R.id.check3);check4 = (CheckBox) findViewById(R.id.check4);button1 = (Button) findViewById(R.id.button1);//多选框点击事件/*check1.setOnCheckedChangeListener(listenter);check2.setOnCheckedChangeListener(listenter);check3.setOnCheckedChangeListener(listenter);check4.setOnCheckedChangeListener(listenter);*///提交按钮点击事件button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {String str = "我学过了";boolean f = false;if(check1.isChecked()){str += check1.getText()+",";f = true;}if(check2.isChecked()){str += check2.getText()+",";f = true;}if(check3.isChecked()){str += check3.getText()+",";f = true;}if(check4.isChecked()){str += check4.getText()+",";f = true;}if(f){str = str.substring(0, str.length()-1);}Toast.makeText(CheckActivity.this, str, Toast.LENGTH_LONG).show();}});}
}

 


http://chatgpt.dhexx.cn/article/LtuV2sqo.shtml

相关文章

Android 之 RadioButton (单选按钮) Checkbox (复选框)

本节引言&#xff1a; 本节给大家带来的是Andoird基本UI控件中的RadioButton和Checkbox; 先说下本节要讲解的内容是&#xff1a;RadioButton和Checkbox的1.基本用法 2.事件处理&#xff1b; 3.自定义点击效果&#xff1b; 4.改变文字与选择框的相对位置&#xff1b; 5.修改文字…

FTP服务器-vsftpd

FTP服务基础 FTP&#xff08;File Transfer Protocol,文件传输协议&#xff09;是典型的C/S结构的应用层协议&#xff0c;需要由服务端软件、客户端软件两个部分共同实现文件传输功能。 FTP连接及传输模式 FTP服务器默认使用TCP协议的20、21端口与客户端进行通信。 20端口用…

ftpvsftpd服务

一. ftp ftp即文件传输,它是INTERNET上仍然常用的最老的网络协议之一,它为系统提供了通过网络与远程服务器传输的简单方法,FTP服务器包的名称为vsftpd 二. 部署ftp服务 1. 安装ftp yum install -y vsftpd #安装vsftpd软…

VSFTPD服务器配置

文章目录 一、实验环境二、实验目的三、实验任务任务一&#xff1a;根据以下要求配置服务器1&#xff09; 配置FTP匿名用户的主目录为/var/ftp,该账户只能下载&#xff0c;不能上传。2&#xff09;建立一个名为abc、口令xyz的FTP账户&#xff0c;设置账户具有上传、下载权限。3…

ftpd服务常见配置总结(/etc/vsftpd/vsftpd.conf)

命令端口&#xff1a; listen_port21 注意&#xff1a;该信息默认没有&#xff0c;使用的话&#xff0c;需要用户自己添加。默认是21。 主动模式端口&#xff1a; connect_from_port_20YES 主动模式端口为20&#xff08;该项配置文件默认已经添加&#xff09; ftp_data_por…

Linux系统中vsftpd配置及如何添加vsFTPd用户和设置权限

vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序。特点是小巧轻快&#xff0c;安全易用。 如果你想在你的Linux/Unix服务器上搭建一个安全、高性能、稳定性好的FTP 服务器&#xff0c;那么vsftpd可能是你的首选应用。vsftpd意思为“very secure FTP daemon(非常安全的FTP…

VSFTPD

首先&#xff0c;下载Metasploitable2-linux,在虚拟机点击文件&#xff0c;然后直接打开。 然后开启此虚拟机&#xff0c;默认用户名和密码都是msfadmin&#xff0c; 登录之后输入ifconfig查看靶机ip 打开kali&#xff0c;打开终端模拟器 输入sudo su&#xff0c;再输入用户密…

vsftp

一、概述 FTP是file Transfer Protocoll文件传输下ieyi&#xff0c;用于Internet上的文件的双向传输。因ftp是明文传输&#xff0c;没有受到保护&#xff0c;所以具有一定危险性。 VSFTP是一个基于GPL发布的类unix系统上使用的FTP服务器软件。为了解决ftp传输安全性问题的&…

vsftpd的使用

简介 vsftpd是 “very secure FTP deamon”的缩写&#xff0c;是一个完全免费&#xff0c;开源的ftp服务器软件。 特点 小巧轻快&#xff0c;安全易用&#xff0c;支持虚拟用户、支持带宽限制等功能。 安装 执行 yum -y install vsftpd 注&#xff1a;在安装前需提前确认是…

vsftpd使用指北

vsftpd使用指北 文章目录 vsftpd使用指北1.安装vsftpd2.登录3.切换本地路径下载命令&#xff1a;get用于下载单个文件&#xff1a;mget用于批量下载&#xff1a;上传命令&#xff1a; FilezillaReference vsftpd 是“very secure FTP daemon”的缩写&#xff0c;是一个完全免费…

linux安装vsftpd

linux安装vsftpd 安装vsftpd作为附件上传和下载服务 文章目录 linux安装vsftpd一、vsftpd是什么&#xff1f;二、使用步骤1.安装2.配置3.可能出现的问题第一种情况&#xff1a;selinux的ftp设置第二种情况&#xff1a;可能是防火墙的原因第三种情况&#xff1a;pam.d设置问题 …

1、vsftpd的简介

1、vsftpd的简介 vsftpd是“very secure TTP daemon”的缩写&#xff0c;是一个完全免费的、开放源代码的ftp服务器软件 2、特点 vsftpd是一款在Linux发行版中最受推崇的ftp服务器程序&#xff0c;小巧轻快&#xff0c;安全易用&#xff0c;支持虚拟用户&#xff0c;支持带宽…

vsftpd基本使用

ftp: 文件传输协议 两类连接&#xff1a; 命令连接&#xff1a;传输命令 数据连接&#xff1a;传输数据 两种模式&#xff1a; 主动模式&#xff1a;PORT 20/tcp连接客户端的命令连接使用的端口向后的第一个可用端口 被动模式&#xff1a;PASV 打开一个随机端口&#xff0c;并…

vsftpd部署流程和常见问题详解

vsftpd部署流程和常见问题详解 ⭐️ 网上关于在云服务器里配置vsftpd的文章鱼龙混杂&#xff0c;没有一篇是可以彻底解决问题的&#xff0c;有些问题虽简单&#xff0c;但也让初学者感到困惑。本文详细说明vsftpd的部署流程和一些常见问题的解决方法&#xff0c;详述用户创建过…

Linux vsFTPd服务详解——vsFTPd基础知识

今天继续给大家介绍Linux运维相关内容&#xff0c;本文主要内容是Linux的vsFTPd服务。 一、vsFTPd服务简介与安装 FTP服务&#xff0c;即File Transfer Protocol、文件传输服务&#xff0c;用于在互联网上提供文件存储和访问服务。有关FTP的原理请参见以下文章&#xff1a;FT…

vsftpd的安装和使用

目录 1、vsftpd的简介... 2 2、特点... 2 3、安装... 2 4、创建虚拟用户... 2 5、vsftpd服务器的配置... 4 6、vsftpd配置文件说明... 7 7、防火墙的配置... 8 8、vsftpd的验证... 9 9、vsftpd的常用命令... 10 10、反复需要验证ftp身份问题解决... 10 1、vsftpd的简…

Number、parseInt和parseFloat的区别

1、Number() 可以把任何类型的数据转换为数值或NaN(not a number)&#xff0c;parseInt()和parseFloat()只能把字符串或数值转换为数值&#xff1b; 2、Number()和parseFloat()会有效解析浮点数&#xff08;如"324.11abc"转为324.11&#xff09;&#xff0c;而parse…

parseFloat()函数的使用

定义&#xff1a; parseFloat() 函数可解析一个字符串&#xff0c;并返回一个浮点数。该函数指定字符串中的首个字符是否是数字。如果是&#xff0c;则对字符串进行解析&#xff0c;直到到达数字的末端为止&#xff0c;然后以数字返回该数字&#xff0c;而不是作为字符串。 语…

Float.parseFloat()的作用

parseFloat()方法用于返回与给定String表示形式相对应的float值 类型转换: string转float

java float.parsefloat_java.lang.Float.parseFloat()方法实例

全屏 java.lang.Float.parseFloat()方法返回一个为指定String表示新的浮点初始化值&#xff0c;由Float类的valueOf方法执行的值。 声明 以下是java.lang.Float.parseFloat()方法的声明public static float parseFloat(String s) throws NumberFormatException 参数s -- 这是要…