1. 程式人生 > >2.2Android程式設計權威指南第二章程式碼

2.2Android程式設計權威指南第二章程式碼

在這裡插入圖片描述
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