Android APP编写简单答题器
作者:lijiao 发布时间:2021-05-29 13:00:17
标签:Android,APP,答题器
本文为大家分享了Android APP编写的简单答题器,此答题器可以通过Next按钮选择下一题,新写题目的类Question,有两个成员变量。
java代码:
package com.android.testrecord;
/**
* Created by wang on 16-10-19.
*/
public class Question {
private int mTextResId;
private boolean mAnswerTrue;
public Question(int textResId, boolean answerTrue) {
mTextResId = textResId;
mAnswerTrue = answerTrue;
}
public int getTextResId() {
return mTextResId;
}
public boolean isAnswerTrue() {
return mAnswerTrue;
}
public void setTextResId(int textResId) {
mTextResId = textResId;
}
public void setAnswerTrue(boolean answerTrue) {
mAnswerTrue = answerTrue;
}
}
java代码:
package com.android.testrecord;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;
private Question[] mQuestionBank = new Question[] {
new Question(R.string.question_oceans, true),
new Question(R.string.question_mideast, false),
new Question(R.string.question_africa, false),
new Question(R.string.question_americas,true),
new Question(R.string.question_asia, true),
};
private int mCurrentIndex = 0;
private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userProessedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
int messageId = 0;
if (userProessedTrue == answerIsTrue)
messageId = R.string.correct_toast;
else
messageId = R.string.incorrect_toast;
Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView) findViewById(R.id.question_test_view);
// int question = mQuestionBank[mCurrentIndex].getTextResId();
// mQuestionTextView.setText(question);
updateQuestion();
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Does nothing yet, but soon!
/* Toast.makeText(QuizActivity.this,
R.string.incorrect_toast,
Toast.LENGTH_SHORT).show(); */
checkAnswer(true);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Does nothing yet, but soon!
/* Toast.makeText(QuizActivity.this,
R.string.correct_toast,
Toast.LENGTH_SHORT).show(); */
checkAnswer(false);
}
});
mNextButton = (Button) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
// int question = mQuestionBank[mCurrentIndex].getTextResId();
// mQuestionTextView.setText(question);
updateQuestion();
}
});
}
}
xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/question_test_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/true_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/true_button"/>
<Button
android:id="@+id/false_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/false_button"/>
</LinearLayout>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next_button"/>
</LinearLayout>
代码:
<resources>
<string name="app_name">GeoQuiz</string>
<string name="question_text">
Constantinople is the largest city in Turkey.
</string>
<string name="true_button">True</string>
<string name="false_button">False</string>
<string name="correct_toast">Correct!</string>
<string name="incorrect_toast">Incorrect!</string>
<string name="action_settings">Settings</string>
<string name="next_button">Next</string>
<string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string>
<string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string>
<string name="question_africa">The source of the Nile River is in Egypt.</string>
<string name="question_americas">The Amazon River is the longest river in the Americas.</string>
<string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake.</string>
</resources>
0
投稿
猜你喜欢
- package com.videobackend.controller;import java.io.File;import java.io
- Activity非异常情况下的生命周期是指,用户正常参与UI交互的情况下,Activity所经过的生命周期的改变;一般情况下,Activit
- 前言日常开发中,我们可能会碰到需要进行防重放与操作幂等的业务,本文记录SpringBoot实现简单防重与幂等防重放,防止数据重复提交操作幂等
- 一.MyBatis简介1)MyBatis 是一款优秀的持久层框架2)MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结
- 遇到的问题解决方法win7:insert键切回win10:fn+insert切回Mac:fn+i 切回来源:https://blog.csd
- 短网址应用已经在全国各大微博上开始流行了起来。例如QQ微博的url.cn,新郎的sinaurl.cn等。我们在QQ微博上发布网址的时候,微博
- 对象持久化是指将内存中的对象保存到可永久保存的存储设备中(如磁盘)的一种技术。本文介绍的是除数据库之外的几种对象持久化方式。具体如下:保存成
- 一.协程概述1.概念协程是Coroutine的中文简称,co表示协同、协作,routine表示程序。协程可以理解为多个互相协作的程序。协程是
- 本文实例为大家分享了java实现KFC点餐系统的具体代码,供大家参考,具体内容如下package KFC点餐系统;//food 类 publ
- instanceof关键字的使用1. 语法格式x instanceof A:检验x是否为类A的对象,返回值为boolean类型,如果是,返回
- 一、简介:开发中在用户注册或找回密码之类的功能,经常会遇到获取短信验证码,获取验证码后需要等待1分钟倒计时,这段时间是不能再次发送短信请求的
- 前言Java 中常规的集合工具,相比大家都熟练于胸,但是如果说有一个集合类你不一定知道或者说肯定没用过,你相不相信呢?今天跟大家介绍的就是&
- 参考资料《Java 编程思想》,关于含有基类的导出类,其成员的初始化过程是一个容易让人困惑的地方,下面通过具体的实例进行讲解,代码取自《Ja
- using System;using System.Collections.Generic;using System.Linq;using
- 批量添加一对多中间表建立中间表A,一个id对应多个lid;传入两条参数long id;//单个数值List lid;//集合数值dao层语句
- Controller代码package com.keafmd.controller;import org.springframework.w
- 目录IO简介1.流Stream 2.IO流的继承结构3 File文件类3.1概述3.2创建对象3.3常用方法 3.4 练
- 本文实例为大家分享了Unity3D UGUI实现缩放循环拖动卡牌展示的具体代码,供大家参考,具体内容如下需求:游戏中展示卡牌这种效果也是蛮酷
- 在 C# 以二进制形式读取数据时使用的是 BinaryReader 类。BinaryReader 类中提供的构造方法有 3 种,具体的语法形
- 学习过java基础,最近趁着大量课余时间想学习Android开发。百度很多资料Android studio,由Google开发的开发工具,那