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>


猜你喜欢
- 本文实例讲述了java多线程下载。分享给大家供大家参考,具体如下:使用多线程下载文件可以更快完成文件的下载,多线程下载文件之所以快,是因为其
- 简单说明一下:线程池可以看做容纳线程的容器;一个应用程序最多只能有一个线程池;ThreadPool静态类通过QueueUserWorkIte
- CircleImageView实现圆形头像代码分享,供大家参考,具体内容如下一、创建属性文件(attrs.xml)具体操作:1、在项目的va
- 这篇文章主要介绍了SpringBoot下如何实现支付宝接口的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值
- 本文实例讲述了Android编程之利用服务实现 * 听的方法。分享给大家供大家参考,具体如下:1. 启动模拟器,部署应用2. 利用模拟器控制
- 枚举中有values方法用于按照枚举定义的顺序生成一个数组,可以用来历遍。我们自定义的枚举类都是继承自java.lang.Enum,拥有一下
- 前言由于现在网络层已经升级到RxJava2.x相关的了,所以需要做些调整。虽然RxJava1.x和RxJava2.x同属RxJava系列,但
- FFmpeg是一个开源免费跨平台的视频和音频流方案,属于自由软件,采用LGPL或GPL许可证(依据你选择的组件)。它提供了录制、转换以及流化
- 以前的Android(4.1之前的版本)中,SDcard跟路径通过“/sdcard”或者“/mnt/sdcard”来表示存储卡,而在Jell
- 今天给大家带来一篇简单易懂的微技巧文章,并没有什么高深的技术点,但重点是在细节,相信可以给不少朋友带来帮助。Dialog和Toast所有人肯
- 前言在开始介绍socket前先补充补充基础知识,在此基础上理解网络通信才会顺理成章,当然有基础的可以跳过去了。都是废话,进入正题。TCP/I
- 1、声明一个测试对象import java.time.LocalDate;import java.util.List;import lomb
- 一、创建项目创建一个简单的Java项目,其中Main.java为主函数,包含main方法:二、完成JAR配置进入File->Proje
- 一、说明标准库提供了许多容器,它们有一个共同点:它们是同类的。也就是说,标准库中的容器只能存储一种类型的元素。 std::vector<
- 二叉堆是一种特殊的堆,二叉堆是完全二元树(二叉树)或者是近似完全二元树(二叉树)。二叉堆有两种:最大堆和最小堆。最大堆:父结点的键值总是大于
- 一、配置逆向generatoe.xml<?xml version="1.0" encoding="UTF
- 什么是异步调用?异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完后才能执行,异步调用则无需等
- 一、Spinner的两种展示样式下拉列表的展示方式有两种,一种是在当前下拉框的正下方展示列表,此时把spinnerMode属性设置为drop
- 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序的数据缓存功能。在Spring Boot应用程序中,我们可
- 背景随着公司业务越来越复杂,在同一个列表中需要展示各种类型的数据。总体结构ItemViewAdapter: 每种类型的卡片分别都是不同的It