一、实验要内容
1. 请给MiniQuiz增加一个Activity,在这个新增的Activity提示答案;
2. 再新增一个Activty,答完最后一个题目的时候,提示用户一共回答正确和错误的汇总情况,以及使用了提示答案的情况;再这个新增的Acity中共用户选择“退出程序”或者“重新开始答题”。
3. 在整个运行过程中,请使用Log显示Activity生命周期状态变化,并在实验报告中分析其状态变化的条件和原因。
4.在以上程序运行的过程中,进行横屏和竖屏的切换,要求,无论程序在什么时候切换,都可以正确运行。
二、结构搭建
三、代码实现
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http:///apk/res/android" xmlns:app="http:///apk/res-auto" xmlns:tools="http:///tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".QuizActivity"> <TextView android:id="@+id/tvQuestion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:drawablePadding="24dp" android:text="题目问题" android:textSize="14sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btnTrue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="True" /> <Button android:id="@+id/btnFalse" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="False" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:orientation="horizontal"> <Button android:id="@+id/btnPrev" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一题" /> <Button android:id="@+id/btnNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一题" /> </LinearLayout> <Button android:id="@+id/btnAnswer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查看答案" /> </LinearLayout>
activity_answer.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http:///apk/res/android" xmlns:app="http:///apk/res-auto" xmlns:tools="http:///tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".AnswerActivity"> <TextView android:id="@+id/tvAnswer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="题目答案" android:textSize="14sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </LinearLayout>
activity_exit.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http:///apk/res/android" xmlns:app="http:///apk/res-auto" xmlns:tools="http:///tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".ExitActivity"> <TextView android:id="@+id/tvTrueFalseAcc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="答对、答错、正确率" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/btnRestart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重新开始" /> <Button android:id="@+id/btnExit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="退出程序" /> </LinearLayout> <TextView android:id="@+id/tvQuestion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="所有题目均已答完" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </LinearLayout>
strings.xml
<resources> <string name="app_name">GeoQuiz</string> <string name="question_australia">Canberra is the capital of Australia</string> <string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean</string> <string name="question_mideast">The Suez Cannal connects the Red Sea and the Indian Oceans</string> <string name="question_africa">The source of the Nile River is in Egypt</string> <string name="question_america">The Amazon River is the longest river in the America</string> <string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake</string> <string-array name="questions"> <item>Canberra is the capital of Australia</item> <item>The Pacific Ocean is larger than the Atlantic Ocean</item> <item>The Suez Canal connects the Red Sea and the Indian Oceans</item> <item>The source of the Nile River is in Egypt</item> <item>The Amazon River is the longest river in the America</item> <item>Lake Baikal is the world\'s oldest and deepest freshwater lake</item> </string-array> <string-array name="answers"> <item>true</item> <item>true</item> <item>false</item> <item>false</item> <item>true</item> <item>true</item> </string-array> </resources>
QuizActivity
package com.example.geoquiz; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; 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 Button mPrevButton; private TextView mQuestionText; private Button mAnswerButton; private static final String TAG = "QuizActivity"; private Boolean[] IsAnswered; private int rightCount = 0; private int wrongCount = 0; private int mCurrentIndex = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.i(TAG, "onCreate() called"); String[] Answers = getResources().getStringArray(R.array.answers); String[] Questions = getResources().getStringArray(R.array.questions); int QuestionCount = Questions.length; IsAnswered = new Boolean[QuestionCount]; mTrueButton = findViewById(R.id.btnTrue); mFalseButton = findViewById(R.id.btnFalse); mQuestionText = findViewById(R.id.tvQuestion); mNextButton = findViewById(R.id.btnNext); mPrevButton = findViewById(R.id.btnPrev); mAnswerButton = findViewById(R.id.btnAnswer); for (int i = 0; i < QuestionCount; i++) { IsAnswered[i] = false; } //判断题目为正确的按钮 mTrueButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String answer = Answers[mCurrentIndex]; IsAnswered[mCurrentIndex] = true; if (answer.equals("true")) { Toast.makeText(QuizActivity.this, "回答正确", Toast.LENGTH_SHORT).show(); rightCount++; } else { Toast.makeText(QuizActivity.this, "回答错误", Toast.LENGTH_SHORT).show(); wrongCount++; } initButtonStatus(); } }); //判断题目为错误的按钮 mFalseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (IsAnswered[mCurrentIndex]) return; String answer = Answers[mCurrentIndex]; IsAnswered[mCurrentIndex] = true; if (answer.equals("true")) { Toast.makeText(QuizActivity.this, "回答错误", Toast.LENGTH_SHORT).show(); wrongCount++; } else { Toast.makeText(QuizActivity.this, "回答正确", Toast.LENGTH_SHORT).show(); rightCount++; } initButtonStatus(); } }); //题目显示 mQuestionText.setText(Questions[mCurrentIndex]); //选择下一题的按钮 mNextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mCurrentIndex = mCurrentIndex + 1; if (mCurrentIndex == QuestionCount) { mCurrentIndex = mCurrentIndex - 1; if (rightCount + wrongCount == QuestionCount) { String result = "共答对了" + rightCount + "题\n" + "答错了" + wrongCount + "题\n" + "正确率为:" + (float) rightCount / QuestionCount * 100 + "%"; Toast.makeText(QuizActivity.this, result, Toast.LENGTH_SHORT).show(); Intent intent = new Intent(QuizActivity.this, ExitActivity.class); intent.putExtra("TrueFalseAcc", result); startActivity(intent); finish(); } } mQuestionText.setText(Questions[mCurrentIndex]); initButtonStatus(); } }); //选择上一题的按钮 mPrevButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mCurrentIndex = (mCurrentIndex - 1); if (mCurrentIndex < 0) mCurrentIndex = 0; mQuestionText.setText(Questions[mCurrentIndex]); initButtonStatus(); } }); //查看答案的按钮 mAnswerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //取出题目和答案 String answer = Answers[mCurrentIndex]; String question = Questions[mCurrentIndex]; //创建QuizActivity类和AnswerActivity类的连接信封 Intent intent = new Intent(QuizActivity.this, AnswerActivity.class); //在intent连接信封中附加消息 intent.putExtra("QuestionAnswer", question + "\n答案为: " + answer); startActivity(intent); Toast.makeText(QuizActivity.this, "答案:" + answer, Toast.LENGTH_SHORT).show(); } }); } //按钮状态不可使用(灰色) private void initButtonStatus() { mTrueButton.setEnabled(!IsAnswered[mCurrentIndex]); mFalseButton.setEnabled(!IsAnswered[mCurrentIndex]); } @Override public void onStart() { super.onStart(); Log.i(TAG, "onStart() called"); } @Override protected void onStop() { super.onStop(); Log.i(TAG, "onStop() called"); } @Override protected void onDestroy() { super.onDestroy(); Log.i(TAG, "onDestroy() called"); } @Override protected void onPause() { super.onPause(); Log.i(TAG, "onPause() called"); } @Override protected void onResume() { super.onResume(); Log.i(TAG, "onResume() called"); } }
Question
package com.example.geoquiz; public class Question { private int mTextResId; private boolean mAnswerTrue; public int getTextResId() { return mTextResId; } public void setTextResId(int textResId) { this.mTextResId = mTextResId; } public boolean isAnswerTrue() { return mAnswerTrue; } public void setAnswerTrue(boolean answerTrue) { this.mAnswerTrue = mAnswerTrue; } public Question(int mTextResId, boolean mAnswerTrue) { this.mTextResId = mTextResId; this.mAnswerTrue = mAnswerTrue; } }
AnswerActivity
package com.example.geoquiz; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class AnswerActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_answer); //获得intent引用 Intent intent = getIntent(); //根据KEY取出value String message = intent.getStringExtra("QuestionAnswer"); //获得文本框引用,设置文字 TextView mAnswerText = findViewById(R.id.tvAnswer); mAnswerText.setText(message); } }
ExitActivity
package com.example.geoquiz; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import androidx.appcompat.app.AppCompatActivity; public class ExitActivity extends AppCompatActivity { private TextView tvQuestion; private Button btnRestart; private Button btnExit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_exit); //获得intent引用 Intent intent = getIntent(); //根据KEY取出value String TrueFalseAcc = intent.getStringExtra("TrueFalseAcc"); //获得文本框引用,设置文字 TextView mTrueFalseAcc = findViewById(R.id.tvTrueFalseAcc); mTrueFalseAcc.setText(TrueFalseAcc); initView(); btnRestart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); startActivity(new Intent(ExitActivity.this, QuizActivity.class)); } }); btnExit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { finish(); } }); } private void initView() { tvQuestion = (TextView) findViewById(R.id.tvQuestion); btnRestart = (Button) findViewById(R.id.btnRestart); btnExit = (Button) findViewById(R.id.btnExit); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http:///apk/res/android" package="com.example.geoquiz"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:screenOrientation="landscape" android:supportsRtl="true" android:theme="@style/Theme.GeoQuiz"> <activity android:name="com.example.geoquiz.ExitActivity" android:exported="false" /> <activity android:name="com.example.geoquiz.AnswerActivity" android:exported="false" /> <activity android:name="com.example.geoquiz.QuizActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
四、运行结果