activity_quiz.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context="com.study.android.geoquizactivity.QuizActivity">
<TextView
android:id="@+id/question_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:text="@string/question_australia"/>
<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>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/front_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/arrow_left"
android:text="@string/Front_button"/>
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="@drawable/arrow_right"
android:text="@string/next_button"/>
</LinearLayout>
</LinearLayout>
strings.xml
<resources>
<string name="app_name">GeoQuizActivity</string>
<string name="question_australia">Canberra is the captical of Australia</string>
<string name="question_oceans">The Pacific Oceans the Red Sea and the Indian 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 Mile River is the Egypt</string>
<string name="question_americas">The Amazon River is the longest river in the Americas</string>
<string name="question_asia">Lake Baikel is the world\'s</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="Front_button">Prev</string>
<string name="next_button">Next</string>
</resources>
QuizActivity.java
public class QuizActivity extends AppCompatActivity {
private Button mTrueButton;
private Button mFalseButton;
private Question[] mQuestionBank=new Question[]{
new Question(R.string.question_australia,true),
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 TextView mQuestionTextView;
private int mCurrentIndex;
private Button mNextButton;
private Button mFrontButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
initView();
initEvent();
}
private void initView() {
//实例化控件
mTrueButton = findViewById(R.id.true_button);
mFalseButton = findViewById(R.id.false_button);
mNextButton = findViewById(R.id.next_button);
mFrontButton = findViewById(R.id.front_button);
mQuestionTextView = findViewById(R.id.question_text_view);
mQuestionTextView.setText(mQuestionBank[mCurrentIndex].getTextResId());
}
private void initEvent() {
//设置匿名内部类监听器
mTrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkAnswer(true);
}
});
mFalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkAnswer(false);
}
});
mFrontButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mCurrentIndex==0) {
Toast.makeText(QuizActivity.this,"已经是第一条了",Toast.LENGTH_SHORT).show();
}else {
mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
mQuestionTextView.setText(mQuestionBank[mCurrentIndex].getTextResId());
}
}
});
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCurrentIndex=(mCurrentIndex+1)%mQuestionBank.length;
mQuestionTextView.setText(mQuestionBank[mCurrentIndex].getTextResId());
}
});
mQuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCurrentIndex=(mCurrentIndex+1)%mQuestionBank.length;
mQuestionTextView.setText(mQuestionBank[mCurrentIndex].getTextResId());
}
});
}
/**
* 将用户输入的结果与正确结果进行对比
* @param userPressedTrue 用户输入的答案
*/
public void checkAnswer(boolean userPressedTrue){
boolean correctAnswer = mQuestionBank[mCurrentIndex].isAnswerTrue();
int messageResId=0;
if(userPressedTrue==correctAnswer) {
messageResId=R.string.correct_toast;
}else {
messageResId=R.string.incorrect_toast;
}
Toast.makeText(QuizActivity.this,messageResId,Toast.LENGTH_SHORT).show();
}
}
PS:可以将Button组件替换成ImageButton,该属性能为视力障碍用户提供方便。在为其设置文字属性值后,如果设备的可访问性选项作了相应设置,那么在用户点击图形按钮时,设备便会读出属性值的内容。
Demo下载地址:
https://download.csdn.net/download/weixin_43953649/10850457