广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现单项、多项选择操作
  • 838
分享到

Android实现单项、多项选择操作

选择Android 2022-06-06 08:06:23 838人浏览 泡泡鱼
摘要

本文实例为大家分享了Android实现单项、多项选择操作的相关代码,供大家参考,具体内容如下 1、单项选择 1.1.布局 <?xml version="1.

本文实例为大家分享了Android实现单项、多项选择操作的相关代码,供大家参考,具体内容如下

1、单项选择
1.1.布局


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="com.rj141.sb.kongjian.DateActivity"> 
 <TextView 
  android:layout_marginLeft="10dp" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="2+3=" 
  android:textSize="22dp" 
  /> 
 <RadioGroup 
  android:layout_marginLeft="20dp" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"> 
  <RadioButton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="A.2" 
   android:id="@+id/rb1" 
   /> 
  <RadioButton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="B.3" 
   android:id="@+id/rb2" 
   /> 
  <RadioButton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="C.4" 
   android:id="@+id/rb3" 
   /> 
  <RadioButton 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:text="D.5" 
   android:id="@+id/rb4" 
   /> 
 </RadioGroup> 
 <Button 
  android:id="@+id/submit" 
  android:layout_width="match_parent" 
  android:layout_height="wrap_content" 
  android:text="提交"/> 
</LinearLayout>

 1.2.Java文件


public class SinGChoose extends AppCompatActivity { 
 private Button btn; 
 private RadioButton rbD; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.sing_choose); 
  rbD= (RadioButton) this.findViewById(R.id.rb4); 
  btn= (Button) this.findViewById(R.id.submit); 
  btn.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
    if(rbD.isChecked()){ 
     Toast.makeText(SingChoose.this,"正确,请加五分",Toast.LENGTH_SHORT).show(); 
    } 
    else { 
     Toast.makeText(SingChoose.this,"错误,请减五分",Toast.LENGTH_SHORT).show(); 
    } 
   } 
  }); 
 } 
} 

效果图:

2多项选择
2.1.布局


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 tools:context="com.rj141.sb.kongjian.CheckChoose"> 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textSize="20dp" 
  android:text="你喜欢下列哪些物品?" 
  /> 
 <CheckBox 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="巧克力" 
  android:id="@+id/cb1" /> 
 <CheckBox 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="冰淇淋" 
  android:id="@+id/cb2" /> 
 <CheckBox 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="蛋糕" 
  android:id="@+id/cb3" /> 
 <CheckBox 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="啤酒" 
  android:id="@+id/cb4" /> 
 <CheckBox 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="肉" 
  android:id="@+id/cb5" /> 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:textSize="18dp" 
  android:id="@+id/tv" /> 
</LinearLayout> 

2.2.Java文件


public class CheckChoose extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { 
 private CheckBox cb1,cb2,cb3,cb4,cb5; 
 private TextView tv; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.check_choose); 
  tv= (TextView) this.findViewById(R.id.tv); 
  cb1= (CheckBox) this.findViewById(R.id.cb1); 
  cb2= (CheckBox) this.findViewById(R.id.cb2); 
  cb3= (CheckBox) this.findViewById(R.id.cb3); 
  cb4= (CheckBox) this.findViewById(R.id.cb4); 
  cb5= (CheckBox) this.findViewById(R.id.cb5); 
  cb1.setOnCheckedChangeListener(this); 
  cb2.setOnCheckedChangeListener(this); 
  cb3.setOnCheckedChangeListener(this); 
  cb4.setOnCheckedChangeListener(this); 
  cb5.setOnCheckedChangeListener(this); 
 } 
 @Override 
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
  String str="您喜欢:"; 
  if(cb1.isChecked()){ 
   str+=cb1.getText()+","; 
  } 
  if(cb2.isChecked()){ 
   str+=cb2.getText()+","; 
  } 
  if(cb3.isChecked()){ 
   str+=cb3.getText()+","; 
  } 
  if(cb4.isChecked()){ 
   str+=cb4.getText()+","; 
  } 
  if(cb5.isChecked()){ 
   str+=cb5.getText()+","; 
  } 
  tv.setText(str); 
 } 
} 

效果图:

您可能感兴趣的文章:Android RadioButton单选框的使用方法android GridView多选效果的实例代码Android checkbox的listView(多选,全选,反选)具体实现方法Android单选按钮对话框用法实例分析基于Android实现点击某个按钮让菜单选项从按钮周围指定位置弹出Android程序开发中单选按钮(RadioGroup)的使用详解Android中创建对话框(确定取消对话框、单选对话框、多选对话框)实例代码


--结束END--

本文标题: Android实现单项、多项选择操作

本文链接: https://www.lsjlt.com/news/25123.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android实现单项、多项选择操作
    本文实例为大家分享了Android实现单项、多项选择操作的相关代码,供大家参考,具体内容如下 1、单项选择 1.1.布局 <?xml version="1....
    99+
    2022-06-06
    选择 Android
  • Android实现选项菜单子菜单
    Android之选项菜单和子菜单学习笔记,供大家参考,具体内容如下 菜单简介: 菜单在桌面应用中使用十分广泛,几乎所有的桌面应用都有菜单。但是随着手机的发展,对于手机桌面菜单的...
    99+
    2022-06-06
    菜单 Android
  • Flutter多项选择弹窗实现详解
    目录多选和单选的不同之处实现方式界面变更代码实现总结在Flutter 底部弹窗详解一篇中介绍了底部弹窗的实现。发出后有在琢磨如何实现多选,这也是很常用的一个功能。本篇介绍实现多选的思...
    99+
    2022-11-12
  • Flutter 底部弹窗如何实现多项选择
    目录多选和单选的不同之处 实现方式 界面变更 代码实现 总结 多选和单选的不同之处 单选的时候,选中一个就可以直接把结果返回,因此本身底部弹窗无需状态管理。但到多选的时候,需要知...
    99+
    2022-11-12
  • 简单实现C# ListBox获取多选项的值
    在C#中,ListBox控件默认情况下是不支持多选的。不过,你可以通过设置SelectionMode属性为SelectionMode...
    99+
    2023-09-22
    C#
  • Android选项菜单用法实例分析
    本文实例讲述了Android选项菜单用法。分享给大家供大家参考。具体如下: Android平台下所提供的菜单大体上可分为三类:选项菜单、上下文菜单和子菜单。 当Activity...
    99+
    2022-06-06
    菜单 Android
  • Android ListView构建支持单选和多选的投票项目
    引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项...
    99+
    2022-06-06
    listview Android
  • Win8右键菜单中的多余选项过多如何选择性的删除
      鼠标右键菜单选项可以说是一个便捷的快捷方式,用户可以通过右键菜单快速开启部分程序,但最近有Win8用户反映鼠标右键菜单的选项越来越多,很多多余选项,有时候要找到一个选项都需要一点时间,如何删除这些选项呢...
    99+
    2022-06-04
    右键 选择性 多余
  • Vue实现简单选项卡功能
    本文实例为大家分享了Vue实现简单选项卡的具体代码,供大家参考,具体内容如下 vue-tab-demo App.vue <template>   <div id=...
    99+
    2022-11-13
  • Vue实现简单选项卡效果
    本文实例为大家分享了Vue实现简单选项卡效果的具体代码,供大家参考,具体内容如下 效果图 1.头部选项卡 点击切换下标ID 传递一个参数回去,active绑定选中样式 <di...
    99+
    2022-11-13
  • Android基于ViewPager Fragment实现选项卡
    先给大家展示效果图: 1.新建TestFragmen继承Fragment public class TestFragment extends Fragment { p...
    99+
    2022-06-06
    viewpager fragment 选项卡 Android
  • Android 实现左滑出现删除选项
    滑动删除的部分主要包含两个部分, 一个是内容区域(用于放置正常显示的view),另一个是操作区域(用于放置删除按钮)。默认情况下,操作区域是不显示的,内容区域的大小是填充整个容 器,操作区域始终位于内容区域的右面。当开始滑动的时候,整个容器...
    99+
    2023-05-31
    android 左滑 删除
  • Android实现拼多多地址选择器
    本文实例为大家分享了Android实现拼多多地址选择器的具体代码,供大家参考,具体内容如下 突然想做一款地区选择器,然后我的弹框用的第三方的,地区数据用的是本地的json文件,解析j...
    99+
    2022-11-12
  • 在html页面的表单选项中怎么实现多选功能
    这篇“在html页面的表单选项中怎么实现多选功能”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这...
    99+
    2022-10-19
  • js下拉菜单语言选项简单实现
    旗子图片 复制代码 代码如下: (function($) { var jSelect = $(".jsSelect"); $(jSelect).find("li:first").ho...
    99+
    2022-11-15
    js 下拉菜单 语言选项
  • 用js实现简单的tab选项卡
    tab选卡 现实网页的使用频率极高,基本上每个网页都需要使用一个或多个tab选卡 我们可以用js实现简单的tab选卡效果 代码如下: <!DOCTYPE html> ...
    99+
    2022-11-12
  • Android使用RecyclerView实现列表数据选择操作
    这些时间做安卓盒子项目,因为安卓电视的显示器比较大,所以一个界面显示 很多数据 ,最多的时候,一个Actvity中用到了好几个RecyclerView。在RecyclerView中实现Item选中处理时,发现用CheckBox的OnChec...
    99+
    2023-05-31
    android recyclerview checkbox
  • Android的ListView多选删除操作实现代码
    最近尝试做了个listview的多选demo,网上看其他人的例子感觉不是很难,自己动手做了下,各种细节问题,没那么简单啊。既然做了,简单写个笔记记录下。 练手demo,命名笔...
    99+
    2022-06-06
    listview Android
  • Android TabLayout(选项卡布局)简单用法实例分析
    本文实例讲述了Android TabLayout(选项卡布局)简单用法。分享给大家供大家参考,具体如下: 我们在应用viewpager的时候,经常会使用TabPageIndic...
    99+
    2022-06-06
    布局 tablayout 选项卡 Android
  • Android如何实现拼多多地址选择器
    这篇文章主要介绍了Android如何实现拼多多地址选择器,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。Android是什么Android是一种基于Linux内核的自由及开放源...
    99+
    2023-06-15
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作