一个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. 洛谷$P5330\ [SNOI2019]$数论 数论

    正解:数论 解题报告: 传送门$QwQ$ ,,,这题还蛮妙的$QwQ$(,,,其实所有数论题对我来说都挺妙的$kk$然后我真的好呆昂我理解了好久$QAQ$ 考虑先建$Q$个点,编号为$[0,Q)$,表 ...

  2. 「洛谷P2397」 yyy loves Maths VI (mode) 解题报告

    P2397 yyy loves Maths VI (mode) 题目背景 自动上次redbag用加法好好的刁难过了yyy同学以后,yyy十分愤怒.他还击给了redbag一题,但是这题他惊讶的发现自己居 ...

  3. Node.js 模块系统入门

    在编程领域中,模块是自包含的功能单元,可以跨项目共享和重用.它们使开发人员的生活更加轻松,因为我们可以使用它来增加应用程序的功能,而不必亲自编写这些功能.它还让我们可以组织和解耦代码,从而使应用程序更 ...

  4. Map and HashMap

    1.1.1. Map 接口 java提供了一组可以以键值对(key-value)的形式存储数据的数据结构,这种数据结构称为Map.我们可以把Map看成一个多行两列的表格,其中第一列存放key,第二列存 ...

  5. DZNEmptyDataSet框架阅读

      前段时间使用公司封装的空白页占位视图工具,工具是对DZNEmptyDataSet框架的封装.这个框架以前在许多项目也都用过,却没有认真阅读过源码,真的很遗憾.这两天趁五一放假有空,将DZNEmpt ...

  6. HTTP,来一个详细的学习。

    HTTP 认识 HTTP 首先你听的最多的应该就是 HTTP 是一种 超文本传输协议(Hypertext Transfer Protocol),这你一定能说出来,但是这样还不够,假如你是大厂面试官,这 ...

  7. js的内存泄漏场景、监控以及分析

    内存泄漏 Q:什么是内存泄漏? 字面上的意思,申请的内存没有及时回收掉,被泄漏了 Q:为什么会发生内存泄漏? 虽然前端有垃圾回收机制,但当某块无用的内存,却无法被垃圾回收机制认为是垃圾时,也就发生内存 ...

  8. Maven 基础(一) | 使用 Maven 的正确姿势

    一.什么是 Maven? Maven 是一个项目管理工具,它的本质是一个项目对象模型(POM),体现在配置中就是我们常见的 pom.xml 文件,而这个 pom 文件就是 Maven 的核心,它管理了 ...

  9. Spring Boot2 系列教程 (三) | 使用 LomBok 提高开发效率

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 上周去了开年会,去的地方是温泉度假村.老实说,我是无感的,90% 是因为没中奖(老板太抠,两百人只抽三个奖),10 ...

  10. cogs 1298. 通讯问题 Tarjan

    1298. 通讯问题 ★★   输入文件:jdltt.in   输出文件:jdltt.out   简单对比时间限制:1 s   内存限制:128 MB [题目描述] 一个篮球队有n个篮球队员,每个队员 ...