700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > android开发系列之性格测试 性格色彩测试android程序开发之十--输出结果

android开发系列之性格测试 性格色彩测试android程序开发之十--输出结果

时间:2021-04-02 18:09:06

相关推荐

android开发系列之性格测试 性格色彩测试android程序开发之十--输出结果

思路:根据用户选择的每道题的选项,进行判断用户性格颜色所占的比例,比例最大的性格色彩即为要返回的结果。而判断用户性格颜色所占的比例的方法也很简单,因为没到性格测试题的选项都是从A-D按照“红”、“蓝”、“黄”、“绿”的顺序进行排列的。

对于存储用户的答案来说,有两种方式,一种是建立本地文件;一种是使用动态数组的方式,把结果直接存在内存中。

我这里选择的是把结果直接存在内存中,然后读取出来计算结果。使用的数组是HashMap,因为对于一道题,也就是一个惟一的题目ID,用户可能会反复修改答案,所以HashMap提供的键值对特性,

put()方法自动用新值替换与指定关键字相关联的原先值,就很好地解决了问题。

但是在使用的过程中,遇到了很严重的错误:空指针异常。

问题出在了HashMap数组的下标初始值是1,而不是0.

性格色彩测试2.0没有实现上下翻页的优化,在3.0中除去了上翻页的按钮。

为了保证测试的准确性,就需要引导用户在读完题目后最好就能做出选择,选择自己第一反应出来的答案,而且一旦选中不能更改。当用户下翻页时,若没有选择某一选项则给予提示,只有用户选中了某一项后,才能跳转到下一页。另外,只有到翻页到最后一题时,并且选择了答案后,才显示测试结果。

在开发3.0版的过程中,遇到一个BUG,当显示测试界面后,点击第一题的答案后,虽然可以实现向下一题的跳转,但是直接从第一题调转到了最后一题。 错误的原因在于:每次点击任何一个单选按钮都会触发事件,如果代码写成如下图

所示的话

,产生的事件即便不是由对应的按钮产生的,也会处触发黄色区域的代码执行。而事实上,每当点击一个单选按钮时,除了翻页外,翻页的事件本身又会触发一个新的事件,因而又会导致黄色区域代码的执行。这样就成了一个循环直至执行到currentNum的值为试题总数结束。

解决的办法就是:保证在触发了特定的事件后才执行黄色区域的代码,也就是把那部分代码分四份分别放在if语句的执行体中。

代码:

package logan.lejia;

import java.util.HashMap;

import android.app.Activity;

import android.content.Intent;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.TextView;

import android.widget.Toast;

public class TestingView extends Activity {

private TextView textview_title;// 题目的标题

private TextView textview;// 显示当前题号和共有多少题目信息

private RadioButton buttonFir, buttonSec, buttonThir, buttonFour;

private RadioGroup radioGroup = null;

private final int version = 1;

private static int currentNum = 1;

SQLiteHelper helper = new SQLiteHelper(TestingView.this, "lejia", null,

version);

// 用于存放用户所选测试题选项

private static HashMap hash = new HashMap();

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

this.setContentView(R.layout.answview);

// 试题的题干

textview_title = (TextView) findViewById(R.id.textView_problems_title);

// 单选按钮和按钮组

buttonFir = (RadioButton) findViewById(R.id.radioButton1);

buttonSec = (RadioButton) findViewById(R.id.radioButton2);

buttonThir = (RadioButton) findViewById(R.id.radioButton3);

buttonFour = (RadioButton) findViewById(R.id.radioButton4);

radioGroup = (RadioGroup) findViewById(R.id.ButtonGroup);

// 两个按钮中间的textView

textview = (TextView) findViewById(R.id.textView_bottomView);

textview.setTextSize(15);

radioGroup

.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup group, int checkedId) {

if (checkedId == buttonFir.getId()) {

hash.put(currentNum + "", 1);// 存信息

if (currentNum != 10) {

turnToNextPage();

} else if (currentNum == 10) {

// 用户答题完毕,显示测试结果

calculateResult();

}

} else if (checkedId == buttonSec.getId()) {

hash.put(currentNum + "", 2);

if (currentNum != 10) {

turnToNextPage();

} else if (currentNum == 10) {

// 用户答题完毕,显示测试结果

calculateResult();

}

} else if (checkedId == buttonThir.getId()) {

hash.put(currentNum + "", 3);

if (currentNum != 10) {

turnToNextPage();

} else if (currentNum == 10) {

// 用户答题完毕,显示测试结果

calculateResult();

}

} else if (checkedId == buttonFour.getId()) {

hash.put(currentNum + "", 4);

if (currentNum != 10) {

turnToNextPage();

} else if (currentNum == 10) {

// 用户答题完毕,显示测试结果

calculateResult();

}

}

System.out.println("onCheckedChanged:" + currentNum);

}

});

// 刚进入界面时,要显示第一题的信息

displayPorblems(currentNum);

}

/**

* 显示测试题信息,需要传入当前的试题号

*

* @param currentNum

*/

private void displayPorblems(int currentNum) {

// 获取可以进行增删改查的数据库实例

SQLiteDatabase database = helper.getWritableDatabase();

Cursor cursor = database.query("test", new String[] { "id", "title",

"first", "second", "third", "fourth" }, "id=?",

new String[] { currentNum + "" }, null, null, null);

cursor.moveToFirst();

// 共有6列,标号从0-5

String title = cursor.getString(1);// 题干

String first = cursor.getString(2);// 选项A

String second = cursor.getString(3);// 选项B

String third = cursor.getString(4);// 选项C

String fourth = cursor.getString(5);// 选项D

cursor.close();

database.close();

textview_title.setText(title);

buttonFir.setText(first);

buttonSec.setText(second);

buttonThir.setText(third);

buttonFour.setText(fourth);

textview.setText("第" + currentNum + "题/共有" + "10" + "题");

}

private void calculateResult() {

int red = 0;

int blue = 0;

int yellow = 0;

int green = 0;

System.out.println(hash.size());

for (int i = 1; i <= hash.size(); i++) {

int selectedItem = hash.get(i + "");

if (selectedItem == 1) {

red = red + 1;

} else if (selectedItem == 2) {

blue = blue + 1;

} else if (selectedItem == 3) {

yellow = yellow + 1;

} else if (selectedItem == 4) {

green = green + 1;

}

}

Intent intent = new Intent();

if (red >= blue) {

if (red >= yellow) {

if (red >= green) {

intent.setClass(TestingView.this, ActivityRed.class);

this.finish();

} else {

intent.setClass(TestingView.this, ActivityGreen.class);

this.finish();

}

} else {

if (yellow >= green) {

intent.setClass(TestingView.this, ActivityYellow.class);

this.finish();

} else {

intent.setClass(TestingView.this, ActivityGreen.class);

this.finish();

}

}

} else {

if (blue >= yellow) {

if (blue >= green) {

intent.setClass(TestingView.this, ActivityBlue.class);

this.finish();

} else {

intent.setClass(TestingView.this, ActivityYellow.class);

this.finish();

}

} else {

if (yellow >= green) {

intent.setClass(TestingView.this, ActivityYellow.class);

this.finish();

} else {

intent.setClass(TestingView.this, ActivityGreen.class);

this.finish();

}

}

}

startActivity(intent);

}

private void turnToNextPage() {

// 下翻页

// 自加

currentNum = currentNum + 1;

System.out.println("turnToNextPage()" + currentNum);

// 刷新测试题

this.displayPorblems(currentNum);

radioGroup.clearCheck();

}

} 答题完毕后效果图:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。