一、实验目的
1.熟练掌握Android开发环境搭建和Android Studio的使用;
2.了解Android Studio中项目的文件目录结构;
3. 掌握基于Activity的Android APP的设计和编码方法,根据需求完成APP界面和Activity之间的数据传递功能。
二、实验内容
1.Android Studio安装与基本使用方法;
2.设计实现一个判断题题库Android APP,其基本功能包括:
1)启动后显示第一个题目的界面,如图1-1所示,界面中包含了题目内容、“正确”、“错误”、“查看答案”和“下一个”按钮。点击“正确”或“错误”表示用于给出的答案,点击“下一个”进入下一个判断题;
2)用户点击“查看”答案后,显示如图1-2所示的界面,其中显示提示信息“确定看答案?”和“答案”按钮。用户点击“答案”后,界面显示该题目的标准答案,如图1-3所示;
3)用户如果在题目界面未选择答案而直接进入答案界面并查看了答案,再次回到题目界面(图1)点击“正确”或“错误”按钮,界面提示信息,如图1-4所示的“作弊是不对的”。
三、实验要求
1.实现上述的基本功能要求。说明:题库内容可以写在代码中。在此基础上,可以进一步优化界面,也可以进一步添加其他功能。
2.实验报告提交内容:
1)结合系统界面(截图)介绍APP功能;
2)核心实现代码(Java或Kotlin代码)和布局文件等。
四、APP功能
一.启动界面中包含了题目内容、“正确”、“错误”、“查看答案”和“下一个”按钮。点击“正确”或“错误”表示用于给出的答案,点击“下一个”进入下一个判断题;
二.用户点击“查看”答案后,弹出提示信息“确定看答案?”和“是/否”按钮。
三.用户如果在题目界面未选择答案而直接进入答案界面并查看了答案,再次回到题目界面点击“正确”或“错误”按钮,界面提示信息“作弊是不对的”。
四.横竖屏幕Activity不刷新。
五.题目循环出现。
五、核心实现代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<TextView
android:id="@+id/question_test_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="20dp"
/>
<Button
android:id="@+id/true_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/true_button"
tools:layout_editor_absoluteX="41dp"
tools:layout_editor_absoluteY="86dp" />
<Button
android:id="@+id/false_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/false_button"
tools:layout_editor_absoluteX="147dp"
tools:layout_editor_absoluteY="85dp" />
<Button
android:id="@+id/check_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/check_answer"
tools:layout_editor_absoluteX="257dp"
tools:layout_editor_absoluteY="85dp" />
<Button
android:id="@+id/next_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/next_button"
tools:layout_editor_absoluteX="257dp"
tools:layout_editor_absoluteY="85dp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.quizactivity;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
private Button mCheckButton;
private Button mNextButton;
private TextView mQuestionTextView;
private int flat=0;//判断是否查看过答案
private Question[] mQuestionBank = new Question[] {
new Question(R.string.question_1, false),
new Question(R.string.question_2, true),
new Question(R.string.question_3, false),
new Question(R.string.question_4,false),
new Question(R.string.question_5, true),
};
private int mCurrentIndex = 0;//题目循环
private void updateQuestion() {
flat=0;
int question = mQuestionBank[mCurrentIndex].getTextResId();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userProessedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
int messageId = 0;
if (userProessedTrue == answerIsTrue && flat != 1)
messageId = R.string.correct_toast;
else
messageId = R.string.incorrect_toast;
if(flat==1)
messageId=R.string.cheat;
Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQuestionTextView = (TextView) findViewById(R.id.question_test_view);
updateQuestion();
mTrueButton = (Button) findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
mCheckButton = (Button) findViewById(R.id.check_answer);
mCheckButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String[] AB = new String[]{"是","否"};
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("确定要查看答案吗");
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
flat=1;
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
if(answerIsTrue == true)
Toast.makeText(MainActivity.this,"答案是:正确",Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this,"答案是:错误",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "再接再厉",Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
});
mNextButton = (Button) findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;//循环题目
updateQuestion();
}
});
}
}
AndroidManifast.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.quizactivity">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.QuizActivity">
<activity
android:name=".MainActivity"
android:exported="true"
android:configChanges="keyboard|keyboardHidden|screenSize|orientation"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Qusetion.java
package com.example.quizactivity;
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;
}
}
strings.xml
<resources>
<string name="app_name">实验1判断</string>
<string name="true_button">对</string>
<string name="false_button">错</string>
<string name="correct_toast">回答正确!</string>
<string name="incorrect_toast">回答错误!</string>
<string name="check_answer">查看答案</string>
<string name="cheat">作弊是不对的!</string>
<string name="next_button">下一题</string>
<string name="question_1">纽约是美国首都。</string>
<string name="question_2">华盛顿是美国首都。</string>
<string name="question_3">元宵节要吃粽子。</string>
<string name="question_4">圣诞节在一月份。</string>
<string name="question_5">十月一是国庆节。</string>
</resources>