上效果图

首先进行控件布局,一个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实现单项选择的更多相关文章

  1. 解剖SQLSERVER 第十七篇 使用 OrcaMDF Corruptor 故意损坏数据库(译)

    解剖SQLSERVER 第十七篇 使用 OrcaMDF Corruptor 故意损坏数据库(译) http://improve.dk/corrupting-databases-purpose-usin ...

  2. Python之路【第十七篇】:Django【进阶篇 】

    Python之路[第十七篇]:Django[进阶篇 ]   Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...

  3. Python之路【第十七篇】:Django之【进阶篇】

    Python之路[第十七篇]:Django[进阶篇 ]   Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...

  4. Python之路【第十七篇】:Django【进阶篇】

    Python之路[第十七篇]:Django[进阶篇 ]   Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...

  5. 单项选择RadioButton和多项选择CheckBox的使用

     在Android中,可以通过RadioButton和RadioGroup的组合来实现单项选择的效果.而多项选择则是通过CheckBox来实现的. 1.单项选择RadioButton 我们知道,一 ...

  6. 阅读《Android 从入门到精通》(10)——单项选择

    单项选择(RadioGroup) RadioGroup 是 LinearLayout 的子类,继承关系例如以下: android.view.ViewGroup android.widget.Linea ...

  7. 跟我学SpringCloud | 第十七篇:服务网关Zuul基于Apollo动态路由

    目录 SpringCloud系列教程 | 第十七篇:服务网关Zuul基于Apollo动态路由 Apollo概述 Apollo相比于Spring Cloud Config优势 工程实战 示例代码 Spr ...

  8. Egret入门学习日记 --- 第十七篇(书中 7.4~8.2节 内容)

    第十七篇(书中 7.4~8.2节 内容) 昨天看到 7.3 节,那么今天. 开始 7.4节.     好吧,这些其他的服务器运行知识,就不搞了... 至此,7.4节 内容结束. 开始 7.5节 内容. ...

  9. 微信小程序实战篇:商品属性联动选择(案例)

    本期的微信小程序实战篇来做一个电商网站经常用到的-商品属性联动选择的效果,素材参考了一点点奶茶. 效果演示:   商品属性联动.gif 代码示例 1.commodity.xml <!-- < ...

随机推荐

  1. python学习笔记(11)--词云

    中分词库  jieba 词云 wordcloud import jieba import wordcloud f = open("新时代中国特色社会主义.txt", "r ...

  2. Shell 编程和Python编程的那些不同之处(一)

    循环 shell中for循环的表现形式: 1.数字段形式 for i in {1..10};do  echo $i;done 还支持按规定的步数进行跳跃的方式实现列表for循环,例如计算1-100内所 ...

  3. Lodop打印控件不打印css背景图怎么办

    background:url()这是css背景图,http协议会按异步方式下载背景图,所以很容易等不到下载完毕就开始打印了,故lodop不打印css背景图.Lodop不打印css背景图,但是有其他方法 ...

  4. 阿里云 ECS 安全组

    以前在案例云买的ECS我一般都是 连上 ssh,然后把网站文件拿上去 ,安装好需要的环境 然后就可以顺利的打开网站了,这次帮一个朋友买的阿里云ECS让我蒙了, 一切都准备好了 网站打不开 防火墙也检查 ...

  5. Nginx 用最快方式让缓存失效

    陶辉103 一般让及时缓存失效针对nginx官方是收费的 我们可以用第三方模块 https://github.com/FRiCKLE/ngx_cache_purge proxy_cache_path ...

  6. css背景色 透明字体不透明

    .demo{ padding: 25px; background-color: rgba(,,,0.5);/* IE9.标准浏览器.IE6和部分IE7内核的浏览器(如QQ浏览器)会读懂 */ }

  7. mysql 自带的性能压力测试工具

    mysqlslap -h127.0.0.1 -uroot -p --concurrency=100 --iterations=1 --auto-generate-sql --auto-generate ...

  8. BZOJ2281[Sdoi2011]黑白棋&BZOJ4550小奇的博弈——DP+nimk游戏

    题目描述 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色不同. 小A可以移动白色 ...

  9. ZOJ 1403 解密

    参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6412212.htmlSafecracker Time Limit: 2 Seconds       ...

  10. CODEFORCES掉RATING记 #5

    ​ 比赛:Codeforces Round #429 (Div. 2) ​ 时间:2017.8.1晚 这次感觉状态不好,就去打div2了 ​ A:有\(26\)种颜色的气球,每种的数量不一样,你要把这 ...