(一)

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. jeakins配置邮件通知,附带解决535报错:authentication failed,如果发现测试邮件可以发出,项目构成无法发出邮件,请开启SSL认证,端口号改为(465),qq邮箱、163邮箱通用

    535报错解决方案:调用163邮箱服务器来发送邮件,我们需要开启POP3/SMTP服务,这时163邮件会让我们设置客户端授权码,这个授权码替代上面代码部分的passwd即可成功发送邮件 如果设置的邮箱 ...

  2. Webapp和后端交互检查测试

    除了功能,我们可以使用下面方法,查看交互过程,页面不能发现的问题: 什么是json 什么是json,json是什么,json如何使用 JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能 ...

  3. Postgres 将查询结果同时插入数据表

    INSERT INTO table [ ( column [, ...] ) ] { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ... ...

  4. js后台提交成功后 关闭当前页 并刷新父窗体(转)

    原文地址:http://www.cnblogs.com/chenghu/p/3696433.html 后台提交成功后 关闭当前页 并刷新父窗体 this.ClientScript.RegisterSt ...

  5. sed处理大txt文件(1G) 比如替换某一串字符串,或者删除一行

    1.将11.sql文件中"prompt"替换为"--prompt",然后保存为111.sql文件 sed -e "s,prompt,--prompt, ...

  6. maven学习(三)——修改maven本地默认仓库

    修改从Maven中心仓库下载到本地的jar包的默认存储位置 从Maven中心仓库下载到本地的jar包的默认存放在”${user.home}/.m2/repository”中,${user.home}表 ...

  7. react-router的原理

    1.hash的方式 以 hash 形式(也可以使用 History API 来处理)为例,当 url 的 hash 发生变化时,触发 hashchange 注册的回调,回调中去进行不同的操作,进行不同 ...

  8. WMS请求GetCapabilities,变成下载mapserv.exe解决办法

    WMS1.1.1和WMS1.3.0两个版本中的几个区别: 1.WMS1.1.1中提供的DescribeLayers.GetStyles等接口在WMS1.3.0中不再提供支持,只提供GetCapabil ...

  9. Windows系统——后缀为.zip.00X的zip分卷解压

    Windows下后缀为*.zip.001文件的解压方法: 后缀为*.zip.001文件用winrar无法解压, 解决办法是在windows下打开命令行界面, 输入:copy /B xx.zip.001 ...

  10. Java简明教程 12.多线程(multithreading)

    单线程和多线程 关于它们的区别,zhihu上有一个回答,我认为十分不错,如下: . 单进程单线程:一个人在一个桌子上吃菜. . 单进程多线程:多个人在同一个桌子上一起吃菜. . 多进程单线程:多个人每 ...