文章目录

  • RadioButton
    • 举例1
    • 举例2
    • 举例三
  • CheckBox
    • 举例

RadioButton

表示单选按钮,是button的子类,每一个按钮都有选择和未选中两种状态,经常与RadioGroup一起使用,否则不能实现其单选功能。
RadioGroup继承自LinearLayout,可以使用Orientation属性控制RadioButton的排列方向

举例1

    <RadioGroupandroid:id="@+id/rg1"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><RadioButtonandroid:id="@+id/rb1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="男"android:textSize="30dp"android:textColor="#3F51B5"android:onClick="rb1"></RadioButton><RadioButtonandroid:id="@+id/rb2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女"android:textSize="30dp"android:textColor="#E91E63"android:onClick="rb2"></RadioButton></RadioGroup><TextViewandroid:id="@+id/tv1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="30dp"></TextView>
    private TextView textView;private RadioButton rb1, rb2;protected void onCreate(Bundle savedInstanceState) {rb1 = (RadioButton) findViewById(R.id.rb1);rb2 = (RadioButton) findViewById(R.id.rb2);textView = (TextView) findViewById(R.id.tv1);}public void rb1(View v){textView.setText("您的性别为男");}public void rb2(View v){textView.setText("您的性别为女");}

举例2

    private RadioGroup radioGroup;private TextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main3);radioGroup=findViewById(R.id.rg1);textView=findViewById(R.id.tv1);radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if (checkedId==R.id.rb1){textView.setText("性别:男");}else if (checkedId==R.id.rb2){textView.setText("性别:女");}}});}

举例三

    @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {RadioButton radioButton=(RadioButton)findViewById(checkedId);String msg=radioButton.getText().toString();textView.setText("性别:"+msg);}});}

CheckBox

表示复选框,是Button的子类,用于实现多选功能。
每个复选框都有“选中”和“未选中”两种状态,这两种状态时通过andorid:checked属性指定的,当该属性的值为true时表示选中状态。

举例

public class MainActivity3 extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{private CheckBox cb1, cb2, cb3;private String hobbys=new String();private Button bt_show;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main3);cb1 = findViewById(R.id.cb1);cb2 = findViewById(R.id.cb2);cb3 = findViewById(R.id.cb3);bt_show=findViewById(R.id.bt_show);cb1.setOnCheckedChangeListener(this);cb2.setOnCheckedChangeListener(this);cb3.setOnCheckedChangeListener(this);bt_show.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity3.this, hobbys, Toast.LENGTH_SHORT).show();}});}/*实现接口CompoundButton.OnCheckedChangeListener接口中的onCheckedChanged方法该方法中的buttonView与isChecked分别表示被点击的控件和选中状态*/@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {String motion=buttonView.getText().toString();/*通过isChecked值判断当前被点击的CheckBox是否为选中状态若为选中则判断hobbys字符串中是否包含此CheckBox的文本信息若不包含,则添加该文本信息到hobbys中如果CheckBox未被选中,则查看hobbys字符串中是否包含该CHeckBox的文本信息若包含,则通过replace方法使用空字符串替换该CheckBox文本信息*/if (isChecked){if (!hobbys.contains(motion)){hobbys=hobbys+motion;}}else {if (hobbys.contains(motion)){hobbys=hobbys.replace(motion,"");}}}
}

RadioButton和CheckBox的使用相关推荐

  1. RadioGroup,RadioButton 和CheckBox

    为什么80%的码农都做不了架构师?>>>    RadioButton和CheckBox的区别: 1.单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox ...

  2. Android开发--RadioButton和CheckBox控件的使用

    在Android的开发中可能会用到RadioButton和CheckBox这两个控件,如果你对MFC开发熟悉的话,相信对这两个控件并不陌生,它们的形状也都是一样的,RadioButton是圆形单选按钮 ...

  3. android开发 问卷调查案例_「Android问卷调查类型页面及逻辑实现」RadioButton、CheckBox、EditView、单选、多选、输入、...

    Android问卷调查类型页面及逻辑实现,RadioButton,CheckBox,EditView,RadioButton+EditView单选.多选.输入.单选加输入四种状态四种类型 问题简述:A ...

  4. Android基础入门教程——2.3.5.RadioButton(单选按钮)Checkbox(复选框)

    Android基础入门教程--2.3.5.RadioButton(单选按钮)&Checkbox(复选框) 标签(空格分隔): Android基础入门教程 本节引言: 本节给大家带来的是Ando ...

  5. 16.Quick QML-ButtonGroup、RadioButton、CheckBox

    本章我们要学习的控件有: ButtonGroup: 按钮组,可以设置RadioButton.CheckBox等成员是否互斥. 即在一个按钮组中只能选中一个. RadioButton: 单选按钮,一组R ...

  6. UI组件之TextView及其子类(二)RadioButton和CheckBox

    单选按钮(RadioButton)和复选框(CheckBox),状态开关按钮(ToggleButton),开关(Switch)都是普通的UI组件,都继承了Button类,因此都可以用Button的各种 ...

  7. Android Studio --- [学习笔记]RadioButton、CheckBox、ImageView、ListView、TCP的三次握手

    说明 源代码 在2.x里有TCP的三次挥手与四次握手,先对它进行简单的回答(百度).预计在下一篇里,会继续说明TCP 接上一篇: Android Studio - > [学习笔记]Button. ...

  8. WinStore控件之Button、HyperlinkButton、RadioButton、CheckBox、progressBar、ScrollViewer、Slider...

    1.Button protected override void OnNavigatedTo(NavigationEventArgs e){/** Button - 按钮控件,其全部功能是通过其基类 ...

  9. Qt学习笔记(3)——PushButton、RadioButton、CheckBox

    2019独角兽企业重金招聘Python工程师标准>>> Qt Creater中有6种Buttons控件,比Qt Designer 多了Command Link Button 和But ...

  10. WPF入门教程详解1——label、TextBlock、Button、Border、RadioButton、CheckBox、 数据绑定、案例、ToolTip、GroupBox、Expand、Fram

    视频地址:https://www.bilibili.com/video/BV1iY411w7zD/?p=3&spm_id_from=pageDriver&vd_source=5dc01 ...

最新文章

  1. 陈百强原来这么帅_朱一龙虽然很少玩手机,但居式打字法很有个性,手指飞舞超级帅!...
  2. 微软中文论坛2010新年Party手记
  3. 二级Access数据库大纲知识要点
  4. php imagemagick 翻译目录
  5. Ubuntu配置NFS服务器与客户端
  6. 免费远程桌面连接工具
  7. H5+ 二维码扫描功能
  8. 高频板和普通PCB板的区别
  9. .net core系列源码地址介绍
  10. 【前端】跨域 问题 原理 + 解决方案 下
  11. Home Barbering Grows In Recession, With Hairy Results
  12. Codeforces Raif Round 1 (Div. 1 + Div. 2) 1428D Bouncing Boomerangs 贪心+构造
  13. SSD NVMe核心之PRP算法
  14. Android静态壁纸和动态壁纸的使用和理解
  15. Android 搜索手机本地的全部视频(通过查询数据库)
  16. maven更换阿里云仓库
  17. 人头识别与计数_目标检测之人头检测(HaarLike Adaboost)---高密度环境下行人检测和统计...
  18. 用Canvas为网页添加动态背景
  19. python词云生成的图片为空白图片
  20. css-动画-旋转大风车

热门文章

  1. 2020-6-9 吴恩达-改善深层NN-w3 超参数调试(3.3 超参数训练的实践:Pandas(资源少,一次一个模型) VS Caviar(资源多,一次多个模型))
  2. 【干货】社交电商精细化运营36计.pdf(附下载链接)
  3. 软件技术雷达 c++
  4. 一个真正的君子应当施恩不图回报
  5. ps入门第6天_ps渐变字_彩色字 案例:彩虹桥or彩虹字特效
  6. mongodb 限制ip访问
  7. 飞凌FCU1103让工业生产更智能-工业物联网网关解决方案
  8. 移动OA办公APP开发应该具有哪些基本功能
  9. [附源码]JAVA+ssm计算机毕业设计村务管理系统(程序+Lw)
  10. Exonerate安装,执行./configure 时出现 aclocal.m4错误