Android编程权威指南2.7挑战练习:为TextView添加监听器

我们将升级GeoQuiz应用,提供更多的地理知识测试题目
功能:
1、用户点击问题或者是Next按钮可以显示下一个问题
2、用户点击True/False按钮,可以即时给出答案正确与否的提示
Android编程权威指南2.7挑战练习:为TextView添加监听器_第1张图片
点击问题后的页面:
Android编程权威指南2.7挑战练习:为TextView添加监听器_第2张图片

activity_main.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_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/question_australia"
        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>

这里的布局组件与GeoGuiz中差不多,只是增加了一个Next按钮

strings.xml

<resources>
    <string name="app_name">GeoQuiz</string>
    <string name="true_button">True</string>
    <string name="false_button">False</string>
    <string name="next_button">Next</string>
    <string name="correct_toast">Correct!</string>
    <string name="incorrect_toast">Incorrect!</string>
    <string name="question_australia">1.Canberra is the capital of Australia.</string>
    <string name="question_oceans">2.The Pacific Ocean is larger than
 the Atlantic Ocean.</string>
    <string name="question_mideast">3.The Suez Canal connects the Red Sea
 and the Indian Ocean.</string>
    <string name="question_africa">4.The source of the Nile River is in Egypt.</string>
    <string name="question_americas">5.The Amazon River is the longest river
 in the Americas.</string>
    <string name="question_asia">6.Lake Baikal is the world\'s oldest and deepest
 freshwater lake.</string>
</resources>

这里最后一个问题中的” ’ “是转义字符,为了显示” ‘ “

Question.java

package com.example.geoquiz;

public class Question {
    private int mTextResId;//保存问题字符串资源ID,资源ID总是int类型
    private  boolean mAnswerTrue;//保存问题的答案

    public int getTextResId() {
        return mTextResId;
    }

    public void setTextResId(int textResId) {
        mTextResId = textResId;
    }

    public boolean isAnswerTrue() {
        return mAnswerTrue;
    }

    public void setAnswerTrue(boolean answerTrue) {
        mAnswerTrue = answerTrue;
    }

    public Question(int textResId, boolean answerTrue){
        mTextResId = textResId;
        mAnswerTrue = answerTrue;

    }
}

将问题和答案都封装在Question类中

MainActivity.java

package com.example.geoquiz;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button mTrueButton;//True按钮
    private Button mFalseButton;//False按钮
    private TextView mQuestionTextView;//问题文本框
    private Button mNextButton;//Next按钮

    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 int mCurrentIndex = 0;//记录数组索引

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        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);//检查用户答案是否正确
            }
        });

        mNextButton = (Button)findViewById(R.id.next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length;//递增索引
                updateQuestion();//更新问题
            }
        });
        mQuestionTextView = (TextView) findViewById(R.id.question_text_view);//引用TextView组件
        mQuestionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length;//递增索引
               updateQuestion();//更新问题
            }
        });
        updateQuestion();//更新问题(不一定放在末尾,可以直接在引用后使用)
    }
    private void updateQuestion(){//更新问题
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mQuestionTextView.setText(question);
    }
    private void checkAnswer(boolean userPressedTrue){//判断用户的答案是否与正确答案一致
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
        if(userPressedTrue == answerIsTrue){
            Toast.makeText(this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(this,R.string.incorrect_toast,Toast.LENGTH_SHORT).show();
        }
    }
}

你可能感兴趣的:(Android)