Android 学习之《Android编程权威指南》第二版 挑战练习(第二章)

2.8 挑战练习:为TextView添加监听器

2.9 挑战练习:添加后退按钮

2.10 挑战练习:从按钮到图标按钮

效果图:

Android 学习之《Android编程权威指南》第二版 挑战练习(第二章)_第1张图片

注意点:

1. TextView也是View的子类,可以为其设置View.OnClickListener监听器
2. 图标按钮组件:ImageButton
3. 后退按钮的监听,不能将索引简单-1,因为这样容易越界,需要注意若-1后的数值小于0则应该加上数组的长度。
4. 布局:
Android 学习之《Android编程权威指南》第二版 挑战练习(第二章)_第2张图片

实现:

Question.java

package com.example.thinkpad.geoquiz;

public class Question { //Question类封装两部分数据:问题文本和问题答案
    private int mTextResId;//用于保存地理知识问题字符串的资源ID,而资源ID总是int类型
    private boolean mAnswerTrue; //本应用中问题答案为true||false
    public Question(int textResId,boolean answerTrue){ //满参构造函数
        mTextResId = textResId;
        mAnswerTrue = answerTrue;
    }
    //get&&set函数
    public int getTextResId() {
        return mTextResId;
    }

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

    public boolean isAnswerTrue() {
        return mAnswerTrue;
    }

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

QuizActivity.java

package com.example.thinkpad.geoquiz;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class QuizActivity extends AppCompatActivity {

    private Button mTrueButton; //true选项按钮
    private Button mFalseButton; //false选项按钮
    private Button mNextButton; //next选项按钮
    private Button mPrevButton; //prev选项按钮
    private ImageButton mImageNextButton; //next图标按钮
    private ImageButton mImagePrevButton; //prev图标按钮
    private TextView mQuestionTextView; //textView文本显示
    private Question[] mQuestionBank = new Question[]{ 
            //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(); //获取资源ID
        mQuestionTextView.setText(question); //设置文本内容
    }

    private void checkAnswer(boolean userPressedTrue){ //检查问题答案函数
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue(); //获取对应问题的答案

        int messageResId = 0;
        //根据答案正确与否分配资源ID
        if(userPressedTrue == answerIsTrue){
            messageResId = R.string.correct_toast;
        }else{
            messageResId = R.string.incorrect_toast;
        }
        Toast.makeText(this,messageResId,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz); //加载布局

        mQuestionTextView = (TextView) findViewById(R.id.question_text_view); //获取TextView对象
        mQuestionTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length; //索引值增加1
                updateQuestion();
            }
        });

        mTrueButton = (Button) findViewById(R.id.true_button); 
        //获取trueButton按钮对象
        mTrueButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAnswer(true);
            }
        });

        mFalseButton = (Button) findViewById(R.id.false_button); 
        //获取falseButton按钮对象
        mFalseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAnswer(false);
            }
        });

        mNextButton = (Button) findViewById(R.id.next_button); 
        //获取NextButton按钮对象
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length; //索引值增加1
                updateQuestion();
            }
        });

        mPrevButton = (Button) findViewById(R.id.prev_button); 
        //获取PrevButton按钮对象
        mPrevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex-1) % mQuestionBank.length;
                if( mCurrentIndex < 0 )mCurrentIndex += mQuestionBank.length;
                //索引值减小1,注意索引不要越界
                updateQuestion();
            }
        });

        mImageNextButton = (ImageButton) findViewById(R.id.img_next_button); 
        //获取ImageNextButton按钮对象
        mImageNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex+1) % mQuestionBank.length; 
                //索引值增加1
                updateQuestion();
            }
        });

        mImagePrevButton = (ImageButton) findViewById(R.id.img_prev_button); 
        //获取ImagePrevButton按钮对象
        mImagePrevButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCurrentIndex = (mCurrentIndex-1) % mQuestionBank.length;
                if( mCurrentIndex < 0 )mCurrentIndex += mQuestionBank.length;
                //索引值减小1
                updateQuestion();
            }
        });
        updateQuestion(); //更新问题
    }
}

strings.xml

<resources>
    <string name="app_name">GeoQuizChallengestring>
    <string name="true_button">TRUEstring>
    <string name="false_button">FALSEstring>
    <string name="prev_button">PREVstring>
    <string name="next_button">NEXTstring>
    <string name="correct_toast">Correct!string>
    <string name="incorrect_toast">Incorrect!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>

activity_quiz.xml


<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.example.thinkpad.geoquiz.QuizActivity">

    <TextView
        android:id="@+id/question_text_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>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/prev_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/arrow_left"
            android:drawablePadding="4dp"
            android:text="@string/prev_button"/>
        <Button
            android:id="@+id/next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/next_button"
            android:drawableRight="@drawable/arrow_right"
            android:drawablePadding="4dp"/>

    LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageButton
            android:id="@+id/img_prev_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow_left"
            android:contentDescription="NEXT"
            />
        <ImageButton
            android:id="@+id/img_next_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arrow_right"
            android:contentDescription="PREV"
            />

    LinearLayout>
LinearLayout>

来自一名刚刚开始学习Android的小菜鸟~

你可能感兴趣的:(Android,《Android编程权威指南》)