Android studio APP开发 单选框和复选框

您所在的位置:网站首页 所选项目的复选框怎么设置出来 Android studio APP开发 单选框和复选框

Android studio APP开发 单选框和复选框

2024-07-17 18:49:26| 来源: 网络整理| 查看: 265

单选框和复选框

单选按钮和复选按钮都是普通按钮Button的子类,所以可以使用所有Button的方法和属性。也有自己特有的属性方法

单选框

单选框就是在多个选项中只选择一个。 在Android中,单选按钮用RadioButton表示,而RadioButton类又是Button子类。 通常情况下,RadioButton组件需要与RadioGroup组件一起使用。

设置单选框

效果 android:checked 为指定选中状态,即设定一个默认选择的按钮。

获取单选框组中选中项的值

通常在以下两种情况下获取单选框组中选中项的值。

在改变单选框组的值时获取在单击其他按钮时获取

获取单选框组选值的基本步骤如下:

找到这个单选框组。通过RadioGroup的id调用setOnCheckedChangeListener方法,根据checkedId来获取被选中的单选按钮。通过getText来获取单选按钮的值进行其他操作 在改变单选框组的值时获取

为了能够清晰展示单选框选择的效果,添加了一个TextView来实时显示单选框获取的值。 修改后的布局管理器如下:

预览 在单选框改变时获取选值需要用到setOnCheckedChangeListener方法。在onCreate中的方法如下:

public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); RadioGroup sex = (RadioGroup) findViewById(R.id.radioGroup1); sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton r = (RadioButton) findViewById(checkedId); String str = r.getText().toString(); TextView textView = (TextView) findViewById(R.id.textshow); textView.setText(str); } }); } }

xiaoguo1 xiaoguo2 xiaoguo3

单击其他按钮时获取

要获取单选按钮组中按钮的值,首先要做的就是在被点击的其他按钮的监听事件onClick中获取单选按钮组中选中按钮的id,再获得其值。获得id的过程可以通过for循环遍历所有单选框,根据isChecked()的方法判断按钮是否被选中,当被选中时,通过getText()来获取值。 代码实现如下:

public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //在改变单选框组的值时获取 final RadioGroup sex = (RadioGroup) findViewById(R.id.radioGroup1); sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { RadioButton r = (RadioButton) findViewById(checkedId); String str = r.getText().toString(); TextView textView = (TextView) findViewById(R.id.textshow); textView.setText(str); } }); //单击其他按钮时获取 Button button = (Button) findViewById(R.id.button01); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str1; for(int i=0;i str1 = r.getText().toString(); TextView textView1 = (TextView) findViewById(R.id.textshow1); textView1.setText(str1); break; } } } }); } }

小锅 效果

复选框

复选框可以进行多项选择,每一个复选框都提供了选中和补选中两种状态。在Android 中,复选框用CheckBox()方法来表示,CheckBox()的子类是,所以可以直接使用Button来实现。

设置复选框

在之前的布局管理器中添加如下代码:

在Activity中添加如下两种方法。并且在onCreate中调用。 为了不让Activity中的代码看起来很乱,所以写在一个方法里。

public void checkBox_button(){ final CheckBox hobby01 = (CheckBox) findViewById(R.id.hobby1); final CheckBox hobby02 = (CheckBox) findViewById(R.id.hobby2); final CheckBox hobby03 = (CheckBox) findViewById(R.id.hobby3); final CheckBox hobby04 = (CheckBox) findViewById(R.id.hobby4); Button button = (Button) findViewById(R.id.button02); hobby01.setOnCheckedChangeListener(checkBox_listener); hobby02.setOnCheckedChangeListener(checkBox_listener); hobby03.setOnCheckedChangeListener(checkBox_listener); hobby04.setOnCheckedChangeListener(checkBox_listener); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String hob = ""; if(hobby01.isChecked()){ hob += hobby01.getText().toString() + " "; } if(hobby02.isChecked()){ hob += hobby02.getText().toString() + " "; } if(hobby03.isChecked()){ hob += hobby03.getText().toString() + " "; } if(hobby04.isChecked()){ hob += hobby04.getText().toString() + " "; } Toast.makeText(SecondActivity.this,hob,Toast.LENGTH_SHORT).show(); } }); } private CompoundButton.OnCheckedChangeListener checkBox_listener = new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ Toast.makeText(SecondActivity.this,buttonView.getText().toString(),Toast.LENGTH_SHORT).show(); } } };

在这里插入图片描述 运行效果如图: 效果



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


    图片新闻

    实验室药品柜的特性有哪些
    实验室药品柜是实验室家具的重要组成部分之一,主要
    小学科学实验中有哪些教学
    计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
    实验室各种仪器原理动图讲
    1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
    高中化学常见仪器及实验装
    1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
    微生物操作主要设备和器具
    今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
    浅谈通风柜使用基本常识
     众所周知,通风柜功能中最主要的就是排气功能。在

    专题文章

      CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭