一个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. 【温故知新】Java web 开发(三)Form表单与上传下载文件

    简介:在一和二的基础之上,这次来记录下如何在页面提交表单数据,以及文件的上传和下载整个流程,请求也不仅限于GET了,也有POST了. 1. 为了方便,在 webapp 下直接新建一个 index.ht ...

  2. 第一次看CCControl

    Control中有九种可能的事件,定义在.h文件中,另外还定义四种状态,用来表示控件高亮等. 在初始化控件的时候: bool Control::init() { if (Layer::init()) ...

  3. $CF908D\ New\ Year\ and\ Arbitrary\ Arrangement$ 期望$dp$

    正解:期望$dp$ 解题报告: 传送门$QwQ$ 阿关于题目里那个形如$ab$的子序列我说下,,,我我我之前$get$了好久$QAQ$.这里子序列的个数的定义是这样儿的,举个$eg$,$aabb$,就 ...

  4. $[NOIp2017]$ 逛公园 $dp$/记搜

    \(Des\) 给定一个有向图,起点为\(1\),终点为\(n\),求和最短路相差不超过\(k\)的路径数量.有\(0\)边.如果有无数条,则输出\(-1\). \(n\leq 10^5,k\leq ...

  5. JVM探秘:四种引用、对象的生存与死亡

    本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. Java虚拟机的内存区域中,程序计数器.Java栈和本地方法栈是线程私有的,随线程而生 ...

  6. 《带你装B,带你飞》pytest修仙之路3 - setup/teardown

    1. 简介 学过unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例开始前和结束后都去执行一次.当然还有更高级一点的setupClass和teardownClass ...

  7. JVM系列五(javac 编译器).

    一.概述 我们都知道 *.java 文件要首先被编译成 *.class 文件才能被 JVM 认识,这部分的工作主要由 Javac 来完成,类似于 Javac 这样的我们称之为前端编译器: 但是 *.c ...

  8. 利用SSH桥接访问服务器的Docker运行的MySql服务

    前情提要 docker的运用越来广泛,许多IT公司都已经将自己的服务跑在Docker上面.在安全性方面又做了一层防护.比如:跑在Docker上的Mysql不做外网端口映射,只能在linux服务器上或进 ...

  9. EntityFramework Core表名原理解析,让我来,揭开你神秘的面纱

    前言 上一节我们针对最开始抛出的异常只是进行了浅尝辄止的解析,是不是有点意犹未尽的感觉,是的,我也有这种感觉,看到这里相信您和我会有一些疑惑,要是我们接下来通过注解.Fluent APi.DbSet分 ...

  10. vs删除空白行 注释

    在vs编辑器中有时需要批量删除无用的空白行,为此,可以使用vs编辑器的查找替换功能: 1. Ctrl+H,打开替换功能框. 2.选择“使用正则表达式”,“当前文档”. 3.在查找框中输入: (?< ...