防止重复回答Android

时间:2018-03-20 14:16:51

标签: java android arrays android-studio

我已经构建了一个测验应用,所以我可以用不同的问题练习自己。我想确保一旦我为特定问题提供答案,问题就会被禁用以防止输入多个答案,我也知道它在Toast部分,但我希望在最后添加最终分数百分比。我非常感谢一些帮助。

package com.example.randomuser.androidquiz;

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

public class QuizActivity extends AppCompatActivity {

private Button mTrueButton;
private Button mFalseButton;
private ImageButton mNextButton;
private ImageButton mPreviousButton;
private TextView mQuestionTextView;
private int mCurrentIndex = 0;
private static final String KEY_INDEX = "index";


private Question[] mQuestionBank = new Question[] {
        new Question(R.string.quiz_questionone, false),
        new Question(R.string.quiz_questiontwo, true),
        new Question(R.string.quiz_questionthree, true),
        new Question(R.string.quiz_questionfour, false),
        new Question(R.string.quiz_questionfive, true),
        new Question(R.string.quiz_questionsix, false),
        new Question(R.string.quiz_questionseven, true),
        new Question(R.string.quiz_questioneight, false),
        new Question(R.string.quiz_questionnine, false),
        new Question(R.string.quiz_questionten, true),
        new Question(R.string.quiz_questionelevn, false),
        new Question(R.string.quiz_questiontwelve, false),
};


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

    if(savedInstanceState != null)
    {
       mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
    }

    mQuestionTextView = (TextView) findViewById(R.id.question_textview);
    updateQuestion();
    mQuestionTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mCurrentIndex = (mCurrentIndex + 1)% mQuestionBank.length;
            updateQuestion();
        }
    });

    mNextButton = (ImageButton) findViewById(R.id.next_button);
    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick( View view ) {
           mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
    });

    mPreviousButton = (ImageButton) findViewById(R.id.previous_button);
    mPreviousButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick( View view ) {
            mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
            if(mCurrentIndex < 0){
                mCurrentIndex = 0;
            }
            updateQuestion();
        }
    });

    mFalseButton = (Button) findViewById(R.id.falseButton);
    mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            checkAnswer(false);
        }
    });
    mTrueButton = (Button) findViewById(R.id.trueButton);
    mTrueButton.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            checkAnswer(true);
     }
    });

}

private void updateQuestion() {
    int question = mQuestionBank[mCurrentIndex].getTextResId();
    mQuestionTextView.setText(question);
}

private void checkAnswer(boolean userGuess)
{
    boolean a = mQuestionBank[mCurrentIndex].isAnswer();
    int messageId = 0;
    if(userGuess == a)
    {
        messageId = R.string.right_answer;
    }
    else{
        messageId = R.string.wrong_answer;
    }

    Toast toast= Toast.makeText(QuizActivity.this, messageId, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.TOP, 0, 300);
    toast.show();
}

@Override
protected void onSaveInstanceState( Bundle outState ) {
    super.onSaveInstanceState(outState);
    outState.putInt(KEY_INDEX, mCurrentIndex);
}

}

0 个答案:

没有答案