第十七篇-使用RadioGroup实现单项选择
上效果图

首先进行控件布局,一个textview,6个radiobutton,
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <TextView
android:id="@+id/textView"
android:layout_width="233dp"
android:layout_height="74dp"
android:textSize="35sp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="25dp" /> <RadioGroup
android:id="@+id/RG"
android:layout_width="264dp"
android:layout_height="202dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
tools:ignore="MissingConstraints"> <RadioButton
android:id="@+id/RB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radiobutton1"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="62dp"
tools:layout_editor_absoluteY="87dp" /> <RadioButton
android:id="@+id/RB2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radiobutton2"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="62dp"
tools:layout_editor_absoluteY="143dp" /> <RadioButton
android:id="@+id/RB3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radiobutton3"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="62dp"
tools:layout_editor_absoluteY="214dp" /> <RadioButton
android:id="@+id/RB4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radiobutton4"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="62dp"
tools:layout_editor_absoluteY="278dp" /> <RadioButton
android:id="@+id/RB5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radiobutton5"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="62dp"
tools:layout_editor_absoluteY="356dp" /> <RadioButton
android:id="@+id/RB6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radiobutton6"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="61dp"
tools:layout_editor_absoluteY="429dp" />
</RadioGroup> </android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.aimee.radiogrouptest; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
TextView textview;
RadioGroup RG;
RadioButton RB1;
RadioButton RB2;
RadioButton RB3;
RadioButton RB4;
RadioButton RB5;
RadioButton RB6; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=findViewById(R.id.textView);
RG=findViewById(R.id.RG);
RB1=findViewById(R.id.RB1);
RB2=findViewById(R.id.RB2);
RB3=findViewById(R.id.RB3);
RB4=findViewById(R.id.RB4);
RB5=findViewById(R.id.RB5);
RB6=findViewById(R.id.RB6); RG.setOnCheckedChangeListener(ChangeRadioGroup);
}
private RadioGroup.OnCheckedChangeListener ChangeRadioGroup=new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==RB1.getId()&&RB1.isChecked()){
textview.setText(RB1.getText());
Toast.makeText(MainActivity.this,RB1.getText()+"被选择",Toast.LENGTH_SHORT).show();
}
else if(checkedId==RB2.getId()&&RB2.isChecked()){
textview.setText(RB2.getText());
Toast.makeText(MainActivity.this,RB2.getText()+"被选择",Toast.LENGTH_SHORT).show(); }
else if(checkedId==RB3.getId()&&RB3.isChecked()){
textview.setText(RB3.getText());
Toast.makeText(MainActivity.this,RB3.getText()+"被选择",Toast.LENGTH_SHORT).show(); }
else if(checkedId==RB4.getId()&&RB4.isChecked()){
textview.setText(RB4.getText());
Toast.makeText(MainActivity.this,RB4.getText()+"被选择",Toast.LENGTH_SHORT).show(); }
else if(checkedId==RB5.getId()&&RB5.isChecked()){
textview.setText(RB5.getText());
Toast.makeText(MainActivity.this,RB5.getText()+"被选择",Toast.LENGTH_SHORT).show(); }
else if(checkedId==RB6.getId()&&RB6.isChecked()){
textview.setText(RB6.getText());
Toast.makeText(MainActivity.this,RB6.getText()+"被选择",Toast.LENGTH_SHORT).show(); }
}
};
}
/res/values/string.xml
<resources>
<string name="app_name">RadioGroupTest</string>
<string name="radiobutton1">Android</string>
<string name="radiobutton2">Sysbian</string>
<string name="radiobutton3">WinCE</string>
<string name="radiobutton4">PalmOS</string>
<string name="radiobutton5">Linux</string>
<string name="radiobutton6">iphoneOS</string>
</resources>
第十七篇-使用RadioGroup实现单项选择的更多相关文章
- 解剖SQLSERVER 第十七篇 使用 OrcaMDF Corruptor 故意损坏数据库(译)
解剖SQLSERVER 第十七篇 使用 OrcaMDF Corruptor 故意损坏数据库(译) http://improve.dk/corrupting-databases-purpose-usin ...
- Python之路【第十七篇】:Django【进阶篇 】
Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...
- Python之路【第十七篇】:Django之【进阶篇】
Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...
- Python之路【第十七篇】:Django【进阶篇】
Python之路[第十七篇]:Django[进阶篇 ] Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...
- 单项选择RadioButton和多项选择CheckBox的使用
在Android中,可以通过RadioButton和RadioGroup的组合来实现单项选择的效果.而多项选择则是通过CheckBox来实现的. 1.单项选择RadioButton 我们知道,一 ...
- 阅读《Android 从入门到精通》(10)——单项选择
单项选择(RadioGroup) RadioGroup 是 LinearLayout 的子类,继承关系例如以下: android.view.ViewGroup android.widget.Linea ...
- 跟我学SpringCloud | 第十七篇:服务网关Zuul基于Apollo动态路由
目录 SpringCloud系列教程 | 第十七篇:服务网关Zuul基于Apollo动态路由 Apollo概述 Apollo相比于Spring Cloud Config优势 工程实战 示例代码 Spr ...
- Egret入门学习日记 --- 第十七篇(书中 7.4~8.2节 内容)
第十七篇(书中 7.4~8.2节 内容) 昨天看到 7.3 节,那么今天. 开始 7.4节. 好吧,这些其他的服务器运行知识,就不搞了... 至此,7.4节 内容结束. 开始 7.5节 内容. ...
- 微信小程序实战篇:商品属性联动选择(案例)
本期的微信小程序实战篇来做一个电商网站经常用到的-商品属性联动选择的效果,素材参考了一点点奶茶. 效果演示: 商品属性联动.gif 代码示例 1.commodity.xml <!-- < ...
随机推荐
- Golang的面向对象实践method
最近在系统的学习go的语法,一切都弄好了之后准备弄个im项目出来玩.在这个过程中会把看到有趣的写法和语法啥的拿出来分析一下. 我一直以为go语言对面向对象没有支持,但是后面看到了类似类的概念,meth ...
- WPF实现Windows资源管理器(附源码)
今天我来写一篇关于利用WPF来实现Windows的资源管理器功能,当然只是局部实现这个功能,因为在很多时候我们需要来实现对本机资源的管理,当然我们可以使用OpenFileDialog dialog ...
- 前端传送JSON数据,报Required request body is missing
声明: 后端为Java,采用SSM框架 前端一个JSON.stringify()传来的json字符串,后端一般用@RequestBody标签来定义一个参数接收 但问题在于,当我使用get方式传JSON ...
- String 常见的十种方法!
public class ZiFuChuan { public static void main(String[] args) { ZiFuChuanFangFa f=new ZiFuChuanFan ...
- python之类和__init__
构建一个商品类,__init__函数类似于构造方法,self类似于this import random class Goods: def __init__(self, name, price): se ...
- 二、kubernetes环境搭建
主要内容 1.环境准备(2主机) 2.安装流程 3.问题分析 4.总结 环境配置(2主机) 系统:CentOS 7.3 x64 网络:局域网(VPC) 主机: master:172.16.0.17 m ...
- React Router 4.0 基本使用
路由的概念,起初来源于服务端,就是当浏览器访问一个网站的不同页面时,服务端能够正确的返回页面的内容.当访问首页时,它能返回首页的内容,访问关于我们页面时,返回关于我们的内容.可以看到路由就是一种控制和 ...
- BZOJ3224普通平衡树——旋转treap
题目: 此为平衡树系列第一道:普通平衡树您需要写一种数据结构,来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数, ...
- POJ1860-Currency Exchange-判正环
两种货币的交换可以当成两条边,建图后跑Bellman_ford算法就好了. Bellman_ford算法可以用来处理负边权,所以可以判断是否存在负环.反过来就可以判断是否存在正环. /*------- ...
- 【BZOJ5119】【CTT2017】生成树计数 DP 分治FFT 斯特林数
CTT=清华集训 题目大意 有\(n\)个点,点权为\(a_i\),你要连接一条边,使该图变成一颗树. 对于一种连边方案\(T\),设第\(i\)个点的度数为\(d_i\),那么这棵树的价值为: \[ ...