一个activity是用来做题用的,效果如下图

在点击下一题时,RadioGroup会默认为第一次选中的状态,造成RadioButton选择混。

解决方案: 第一步:取消监听 radioGroup_problem_group.setOnCheckedChangeListener(null);  radioGroup_problem_group为RadioGroup变量名。

第二部:清除选中记录 radioGroup_problem_group.clearCheck();

主要代码:

1. 布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!--单选题-->
<LinearLayout
android:id="@+id/linearLayout_problem"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:visibility="visible"
> <!--题目标题-->
<LinearLayout
android:id="@+id/linearLayout_problem_problem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
> <!--选择题目题号-->
<TextView
android:id="@+id/textView_problem_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /> <!--选择题目标题-->
<TextView
android:id="@+id/textView_problem_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
/> </LinearLayout> <!--题目选项-->
<LinearLayout
android:id="@+id/linearLayout_problem_checkBox"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
> <!--选项单选-->
<RadioGroup
android:id="@+id/radioGroup_problem_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
> <!--题目选项1 android:background="@drawable/linear_layout_middle_content" -->
<RadioButton
android:id="@+id/radioButton_problem_option1"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> <!--题目选项2-->
<RadioButton
android:id="@+id/radioButton_problem_option2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" /> <!--题目选项3-->
<RadioButton
android:id="@+id/radioButton_problem_option3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" /> <!--题目选项4-->
<RadioButton
android:id="@+id/radioButton_problem_option4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" />
</RadioGroup> </LinearLayout> </LinearLayout> <!--显示已答和总题数信息-->
<LinearLayout
android:id="@+id/linearLayout_problem_information"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:visibility="visible"
> <LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已答/总数 : "
/> <TextView
android:id="@+id/textView_problem_answered_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" / "
/> <TextView
android:id="@+id/textView_problem_all_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </LinearLayout> </LinearLayout> <!--下一题按钮-->
<Button
android:id="@+id/button_problem_next"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/linear_layout_middle_content"
android:visibility="gone"
android:text="下一题"
/> <!--完成按钮-->
<Button
android:id="@+id/button_problem_complete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="@drawable/linear_layout_middle_content"
android:visibility="gone"
android:text="完成"
/> </LinearLayout>

2. activity代码

package com.example.lenovo.computerbase;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.IdRes;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView; import com.example.lenovo.computerbase.bean.HttpAddress;
import com.example.lenovo.computerbase.bean.Problem;
import com.example.lenovo.computerbase.bean.ShowToast;
import com.example.lenovo.computerbase.controller.ActivityCollectorController;
import com.example.lenovo.computerbase.http.HttpConnection;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import okhttp3.Call;
import okhttp3.Response; /*
* 测试练习
* */
public class TestActivity extends AppCompatActivity implements View.OnClickListener{ //返回和退出布局LinearLayout
private LinearLayout linearLayout_title_back,linearLayout_title_close;
//返回和退出布局ImageView
private ImageView imageView_title_back, imageView_title_close;
//顶部中间显示文字区域
private TextView textView_title_text; private TextView textView_problem_number, textView_problem_title;
private RadioGroup radioGroup_problem_group;
private RadioButton radioButton_problem_option1, radioButton_problem_option2,
radioButton_problem_option3, radioButton_problem_option4;
private TextView textView_problem_answered_total, textView_problem_all_total;
private Button button_problem_next, button_problem_complete; private ProblemHandler problemHandler = new ProblemHandler(this); HttpAddress httpAddress = new HttpAddress();
private String loginId; private int index = 0;
ArrayList<Problem> problems = new ArrayList<>();
private int length;
private int textColor; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test); httpAddress = (HttpAddress) getApplication();
loginId = getIntent().getStringExtra("loginId");
initView(); requestProblem(); } public void initView(){
//将该Activity加入活动集合控制器
ActivityCollectorController.addActivity(this); //将导航栏隐藏
ActionBar actionBar = getSupportActionBar();
if (actionBar != null){
actionBar.hide();
} //返回
linearLayout_title_back = (LinearLayout)findViewById(R.id.linearLayout_title_back);
linearLayout_title_back.setOnClickListener(this); //退出
linearLayout_title_close = (LinearLayout)findViewById(R.id.linearLayout_title_close);
linearLayout_title_close.setOnClickListener(this); //返回和退出布局ImageView
imageView_title_back = (ImageView)findViewById(R.id.imageView_title_back);
imageView_title_back.setColorFilter(this.getResources().getColor(R.color.colorWhite));
imageView_title_close = (ImageView)findViewById(R.id.imageView_title_close);
imageView_title_close.setColorFilter(this.getResources().getColor(R.color.colorWhite)); //顶部中间显示文字区域
textView_title_text = (TextView)findViewById(R.id.textView_title_text);
textView_title_text.setText("随机练习");
textView_title_text.setTextColor(this.getResources().getColor(R.color.colorWhite)); textView_problem_number = (TextView) findViewById(R.id.textView_problem_number);
textView_problem_title = (TextView) findViewById(R.id.textView_problem_title); radioGroup_problem_group = (RadioGroup) findViewById(R.id.radioGroup_problem_group); radioButton_problem_option1 = (RadioButton) findViewById(R.id.radioButton_problem_option1);
radioButton_problem_option2 = (RadioButton) findViewById(R.id.radioButton_problem_option2);
radioButton_problem_option3 = (RadioButton) findViewById(R.id.radioButton_problem_option3);
radioButton_problem_option4 = (RadioButton) findViewById(R.id.radioButton_problem_option4);
/* 得到RadioButton文字显示的默认颜色(黑色)
* */
textColor = radioButton_problem_option1.getCurrentTextColor(); textView_problem_answered_total = (TextView) findViewById(R.id.textView_problem_answered_total);
textView_problem_all_total = (TextView) findViewById(R.id.textView_problem_all_total); button_problem_next = (Button) findViewById(R.id.button_problem_next);
button_problem_next.setOnClickListener(this);
button_problem_complete = (Button) findViewById(R.id.button_problem_complete);
button_problem_complete.setOnClickListener(this); } private static class ProblemHandler extends Handler{
private final WeakReference<TestActivity> weakReference; public ProblemHandler(TestActivity testActivity){
weakReference = new WeakReference<>(testActivity);
} @Override
public void handleMessage(Message message){
if (weakReference.get() == null)
return; // something
weakReference.get().problems = message.getData().getParcelableArrayList("problems");
weakReference.get().length = weakReference.get().problems.size();
weakReference.get().showProblems(weakReference.get().index);
}
}

  //进行网络请求
public void requestProblem(){
httpAddress.setAddress("RandomTestServlet");
HttpConnection.sendOkHttpRequest(httpAddress.getAddress(), new okhttp3.Callback(){ @Override
public void onFailure(Call call, IOException e) {
Log.d("TestActivity", "请求服务器失败!");
} @Override
public void onResponse(Call call, Response response) throws IOException {
String responseData = response.body().string();
System.out.println(responseData);
ArrayList<Problem> problems = new Gson().fromJson(responseData, new TypeToken<ArrayList<Problem>>(){}.getType());
putProblemHandler(problems);
}
});
} public void putProblemHandler(ArrayList<Problem> problems){
Message message = Message.obtain();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("problems", problems);
message.setData(bundle);
problemHandler.sendMessage(message);
}

  //通过下标取出单个Problem
public void showProblems(int index){ if (index < length){
showProblem(problems.get(index));
} }

  //在页面上显示题目信息
public void showProblem(Problem problem){
textView_problem_number.setText(String.valueOf(problem.getP_no()));
textView_problem_title.setText(problem.getP_title()); radioButton_problem_option1.setText("A. " + problem.getP_option1()); radioButton_problem_option2.setText("B. " + problem.getP_option2()); radioButton_problem_option3.setText("C. " + problem.getP_option3()); radioButton_problem_option4.setText("D. " + problem.getP_option4()); /* 将RadioButton字体颜色设置为默认,是为了消除上一次选中留下的记录
* */
radioButton_problem_option1.setTextColor(textColor);
radioButton_problem_option2.setTextColor(textColor);
radioButton_problem_option3.setTextColor(textColor);
radioButton_problem_option4.setTextColor(textColor);

    // 显示已答题数和全部题数
showAnsweredTotal(index, length); RadioGroupProblemListener radioGroupProblemListener = new RadioGroupProblemListener(problem);
    //为RadioGroup设置监听
radioGroup_problem_group.setOnCheckedChangeListener(radioGroupProblemListener); } // 内部类(设置监听)
class RadioGroupProblemListener implements RadioGroup.OnCheckedChangeListener{ private Problem problem; RadioGroupProblemListener(Problem problem){
this.problem = problem;
} @Override
public void onCheckedChanged(RadioGroup radioGroup, int id) { //选择选项1
if (id == radioButton_problem_option1.getId()) { /*
答案和选项1相等时,选项1显示GREEN颜色
否则,选项1显示RED颜色
并分别判断答案和其余每个选项是否相等,相等显示GREEN颜色
*/
if (problem.getP_answer().equals(problem.getP_option1())){
radioButton_problem_option1.setTextColor(Color.GREEN);
}else {
radioButton_problem_option1.setTextColor(Color.RED); if (problem.getP_answer().equals(problem.getP_option2())){
radioButton_problem_option2.setTextColor(Color.GREEN);
}else if (problem.getP_answer().equals(problem.getP_option3())){
radioButton_problem_option3.setTextColor(Color.GREEN);
}else if (problem.getP_answer().equals(problem.getP_option4())){
radioButton_problem_option4.setTextColor(Color.GREEN);
}
} lastProblem(); }
//选择选项2
else if (id == radioButton_problem_option2.getId()) { if (problem.getP_answer().equals(problem.getP_option2())){
radioButton_problem_option2.setTextColor(Color.GREEN);
}else {
radioButton_problem_option2.setTextColor(Color.RED); if (problem.getP_answer().equals(problem.getP_option1())){
radioButton_problem_option1.setTextColor(Color.GREEN);
}else if (problem.getP_answer().equals(problem.getP_option3())){
radioButton_problem_option3.setTextColor(Color.GREEN);
}else if (problem.getP_answer().equals(problem.getP_option4())){
radioButton_problem_option4.setTextColor(Color.GREEN);
}
} lastProblem(); }
//选择选项3
else if (id == radioButton_problem_option3.getId()) { if (problem.getP_answer().equals(problem.getP_option3())){
radioButton_problem_option3.setTextColor(Color.GREEN);
}else {
radioButton_problem_option3.setTextColor(Color.RED); if (problem.getP_answer().equals(problem.getP_option1())){
radioButton_problem_option1.setTextColor(Color.GREEN);
}else if (problem.getP_answer().equals(problem.getP_option2())){
radioButton_problem_option2.setTextColor(Color.GREEN);
}else if (problem.getP_answer().equals(problem.getP_option4())){
radioButton_problem_option4.setTextColor(Color.GREEN);
}
} lastProblem(); }
//选择选项4
else if (id == radioButton_problem_option4.getId()) { if (problem.getP_answer().equals(problem.getP_option4())){
radioButton_problem_option4.setTextColor(Color.GREEN);
}else {
radioButton_problem_option4.setTextColor(Color.RED); if (problem.getP_answer().equals(problem.getP_option1())){
radioButton_problem_option1.setTextColor(Color.GREEN);
}else if (problem.getP_answer().equals(problem.getP_option2())){
radioButton_problem_option2.setTextColor(Color.GREEN);
}else if (problem.getP_answer().equals(problem.getP_option3())){
radioButton_problem_option3.setTextColor(Color.GREEN);
}
} lastProblem(); }
}
}

   //判断是否最后一题,最后一题时显示完成按钮,否则显示下一题按钮
public void lastProblem(){
System.out.println("index = " + index);
System.out.println("length = " + length); showAnsweredTotal(index + 1, length); // 最后一题
if (index == length - 1){
System.out.println("index == length - 1 ");
button_problem_complete.setVisibility(View.VISIBLE);
}else {
System.out.println("index != length - 1 ");
button_problem_next.setVisibility(View.VISIBLE);
}
} public void showAnsweredTotal(int i, int j){
// 已答题数
textView_problem_answered_total.setText(String.valueOf(i));
textView_problem_answered_total.setTextColor(Color.GREEN);
// 全部题数
textView_problem_all_total.setText(String.valueOf(j));
} /* 设置恢复RadioGroup默认状态
* */
public void defaultRadioGroup(){
/*
* 循环使用RadioGroup时需要先取消监听,再清除选中记录
* */
// 1. 取消监听
radioGroup_problem_group.setOnCheckedChangeListener(null);
// 2. 清除选中记录
radioGroup_problem_group.clearCheck(); } @Override
public void onClick(View view) {
switch (view.getId()){
//返回
case R.id.linearLayout_title_back:
finish();
break;
//退出
case R.id.linearLayout_title_close:
ActivityCollectorController.finishAll();
Intent closeIntent = new Intent(getBaseContext(),LoginActivity.class);
startActivity(closeIntent);
new ShowToast(this,"您已退出!").showToast();
break;
case R.id.button_problem_next:
          //题目索引加一
index ++;
button_problem_next.setVisibility(View.GONE); // 清除上次选中记录
defaultRadioGroup(); showProblems(index);
// 2. 重新请求problem
break;
case R.id.button_problem_complete:
button_problem_complete.setVisibility(View.GONE);
finish();
break;
default: }
}
}

在一个Activity中循环使用一组RadioGroup的更多相关文章

  1. Android在一个Activity中关闭另一个Activity

    比如有ActivityA, ActivityB,在ActivityB中关闭ActivityA. 解决方案: 1.在 ActivityA 里面设置一个静态的变量instance,初始化为this,在 A ...

  2. Android开发中在一个Activity中关闭另一个Activity

    比如有ActivityA, ActivityB,在ActivityB中关闭ActivityA 解决方案: 1. 在 ActivityA 里面设置一个静态的变量instance,初始化为this在 Ac ...

  3. 在一个activity中销毁指定activity

    通过静态变量的方法: 1.在Aactivity中设置一个Activity静态变量 static Activity activity; 2.在onCreate中: activity=this: 3.在B ...

  4. 在一个Activity中启动另一个Activity

    一.新建一个空的工程 二.添加一个Activity并命名为BAty 三.在activity_main.xml中添加一个按钮,设置id号为btnStartB <Button android:lay ...

  5. 多个Fragment在一个activity中通过按钮的展示方法

    fragment使用方法 1. 创建主Mainactivity extends AppCompatActivity 2. Oncreate & setContentView 3. 完成XML的 ...

  6. 在一个apk中调用另外一个apk中的activity

    今天忽然想到如果要在一个activity中调用另外一个activity该怎么办呢? 感觉这个应该比较简单,应为activity的启动方式就两种:显式启动.隐式启动: 显式启动的话肯定不行,那就只能使用 ...

  7. android开发之在activity中控制另一个activity的UI更新

    转自:http://blog.csdn.net/jason0539/article/details/18075293 第一种方法: 遇到一个问题,需要在一个activity中控制另一个acitivit ...

  8. Android 实现在Activity中操作刷新另外一个Activity数据列表

    做android项目中遇到这样一个问题:有两个acticity,一个显示好友列表,另外一个显示会话列表,现在问题是在会话界面增加一个添加好友功能,添加好友后要求实时的刷新好友列表. 想了想,找了两种方 ...

  9. Android笔记(二十) Activity中的跳转和值传递

    我们知道,一个APP是由若干个Activity组成的,那么各个Acitivity中肯定需要进行跳转以及传递数值以保证App的运行,现总结一下多个Activity之间的跳转和值传递. 显式Intent跳 ...

随机推荐

  1. MySQL基础之事务编程学习笔记

    MySQL基础之事务编程学习笔记 在学习<MySQL技术内幕:SQL编程>一书,并做了笔记.本博客内容是自己学了<MySQL技术内幕:SQL编程>事务编程一章之后,根据自己的理 ...

  2. Mysql基本配置以及常见问题

    以下是MYSQL的基本配置以及常见问题的解决方式 1.获取客户端 https://dev.mysql.com/downloads/ 有两种方式获取到MYSQL,一种是通过MySQL Community ...

  3. ELK学习实验003:Elasticsearch 集群安装

    前面已经介绍了Elasticsearch这个工具,并对单机安装做了简单介绍,现在使用三台机器做一个elasticsearch集群 一 环境准备 1.1 机器准备 1.2 同步时间 [root@node ...

  4. 003 ansible部署ceph集群

    介绍:在上一次的deploy部署ceph,虽然出了结果,最后的结果并没有满足最初的目的,现在尝试使用ansible部署一遍,看是否会有问题 一.环境准备 ceph1充当部署节点,ceph2,ceph3 ...

  5. JS进阶——this绑定了谁?

    一.this的意义 二.寻找this绑定对象 经常听到这么一句话,找this只需要看谁是调用方.当函数被调用时会记录函数调用调用方式.传参包括this等各种属性.有时候this绑定对象情况太抽象,找到 ...

  6. 1083 是否存在相等的差 (20 分)C语言

    给定 N 张卡片,正面分别写上 1.2.--.N,然后全部翻面,洗牌,在背面分别写上 1.2.--.N.将每张牌的正反两面数字相减(大减小),得到 N 个非负差值,其中是否存在相等的差? 输入格式: ...

  7. spring boot学习笔记(1)

    一.构建springboot项目 可以通过idea的springboot initialization来创建, idea的功能很强大,可以自己选择需要集成的插件. 完整的项目结构是这样的: DemoA ...

  8. RabbitMQ远程调用测试用例

    RabbitMQ远程调用测试,使用外部机器192.168.174.132上的RabbitMQ,使用之前需要对远程调用进行配置,操作过程见博文“解决RabbitMQ远程不能访问的问题”. SendTes ...

  9. Java 中级 学习笔记 2 JVM GC 垃圾回收与算法

    前言 在上一节的学习中,已经了解到了关于JVM 内存相关的内容,比如JVM 内存的划分,以及JDK8当中对于元空间的定义,最后就是字符串常量池等基本概念以及容易混淆的内容,我们都已经做过一次总结了.不 ...

  10. python报错: invalid syntax

    invalid syntax: 无效的语法. 解决办法:查看当前语句中的  , 如果当前行没找到错误,依次往上找,往上找时可以利用是否有输出进行快速查找. 原因:python语法很严格,少了左括号.右 ...