(一)

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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="helloworld.com.inspur.app5.MainActivity"> <TextView android:layout_width="wrap_content"
android:id="@+id/tv"
android:layout_height="wrap_content"
android:text="选择正确答案:" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rb1"
android:text="2+1=2"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rb2"
android:text="2+1=3"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rb3"
android:text="2+1=4"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rb4"
android:text="2+1=1"/> </RadioGroup> </LinearLayout>

注意事项:RadioButton需要包含在RadioGroup内部才能实现单选的效果

2,逻辑代码的处理

package helloworld.com.inspur.app5;

import android.support.v4.widget.TextViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
private TextView tv;
private RadioGroup rg;
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb4;
private RadioButton rb3; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
rg=(RadioGroup)findViewById(R.id.rg);
rb1=(RadioButton)findViewById(R.id.rb1);
rb2=(RadioButton)findViewById(R.id.rb2);
rb4=(RadioButton)findViewById(R.id.rb4);
rb3=(RadioButton)findViewById(R.id.rb3);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId)
{
case R.id.rb1:
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show(); break;
case R.id.rb2:
Toast.makeText(MainActivity.this,"true",Toast.LENGTH_LONG).show();
break;
case R.id.rb3:
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
break;
case R.id.rb4:
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
break; }
}
}); }
}

注意:

 Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
在后面加上.show()才会显示。

(二)if判断id是否相同

if(R.id.rb2==checkedId)
{
Toast.makeText(MainActivity.this,"true",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
}

(三)if判断对象是否相同

RadioButton r=(RadioButton)findViewById(checkedId);
if(rb2.equals(r))
{
Toast.makeText(MainActivity.this,"true",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this,"false",Toast.LENGTH_LONG).show();
}

OnChencedChang的更多相关文章

  1. 在Windows宿主机中连接虚拟机中的Docker容器

    1. 简单拓扑图

随机推荐

  1. mac攻略(九) -- ssh工具secureCRT

    mac ssh 客户端 : 本身mac直接使用终端来ssh连接就很方便,但是使用过程中随着远程服务器的增多和zsh和远程服务器编码不同产生了乱码,决定安装一款ssh终端软件,以下方法亲测可用,感谢提供 ...

  2. Active Directory 站点和服务

    Active Directory 站点和服务 更新时间: 2012年3月 应用到: Windows Server 2008, Windows Server 2008 R2, Windows Serve ...

  3. 测试环境docker化—容器集群编排实践

    本文来自网易云社区 作者:孙婷婷 背景 在前文<测试环境docker化-基于ndp部署模式的docker基础镜像制作>中已经详述了docker镜像制作及模块部署的过程,按照上述做法已可以搭 ...

  4. Stephen 博客正式开通 【个人公众号:Stephen 】

    个人博客开通. 个人公众号:Stephen

  5. alert(1) to win部分解题

    XSS在线习题分析 (https://alf.nu/alert1) 1. Warmup function escape(s) { return '<script>console.log(& ...

  6. jmeter将上一个请求的结果作为下一个请求的参数——使用正则提取器

    转自:http://www.cnblogs.com/0201zcr/p/5089620.html 在压力测试的时候,经常要将几个流程串联起来才能将程序测试通过.如:我现在用户首先要登录,获得我登录的凭 ...

  7. Win右键管理员权限的获取

    Windows Registry Editor Version 5.00 ;取得文件修改权限 [HKEY_CLASSES_ROOT\*\shell\runas] @="管理员权限" ...

  8. 简单数据结构题(from 钟子谦——IOI2018集训队自选题)

    简单数据结构题(from 钟子谦--IOI2018集训队自选题) 试题描述 给一棵 \(n\) 个点的树,点权开始为 \(0\) ,有 \(q\) 次操作,每次操作是选择一个点,把周围一圈点点权 \( ...

  9. Java EE 学习(8):IDEA + maven + spring 搭建 web(4)- 用户管理

    转载:Gaussic(一个致力于AI研究却不得不兼顾项目的研究生) 注:在阅读本文前,请先阅读: Java EE 学习(5):IDEA + maven + spring 搭建 web(1) ava E ...

  10. 简析JVM GC的根搜索算法

    根搜索算法的基本思路是通过一系列的“GC Roots”的对象作为起始点,从这些节点开始往下搜索,搜索的走过的路径称为引用链,当一个对象到“GC Roots”没有引用链可达时(也就是用图论的话说就是从G ...