首页 > 解决方案 > 显示测验应用的结果 (Android Studio)

问题描述

我有一个可以正常运行的测验应用程序,但问题是用户必须正确回答所有问题才能赢得游戏(如果玩家弄错了游戏将结束)。

我想做的是回答问题,然后最后会有一个活动显示玩家回答了多少,然后会有重试选项并返回菜单

这是 maingameactivity 的代码

public class MainGameActivity extends AppCompatActivity {
FButton buttonA, buttonB, buttonC, buttonD;
TextView questionText, triviaQuizText, timeText, resultText, coinText;
TriviaQuizHelper triviaQuizHelper;
TriviaQuestion currentQuestion;
List<TriviaQuestion> list;
int qid = 0;
int timeValue = 20;
int coinValue = 0;
CountDownTimer countDownTimer;
Typeface tb, sb;

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

    //Initializing variables
    questionText = (TextView) findViewById(R.id.triviaQuestion);
    buttonA = (FButton) findViewById(R.id.buttonA);
    buttonB = (FButton) findViewById(R.id.buttonB);
    buttonC = (FButton) findViewById(R.id.buttonC);
    buttonD = (FButton) findViewById(R.id.buttonD);
    triviaQuizText = (TextView) findViewById(R.id.triviaQuizText);
    timeText = (TextView) findViewById(R.id.timeText);
    resultText = (TextView) findViewById(R.id.resultText);
    coinText = (TextView) findViewById(R.id.coinText);

    //Setting typefaces for textview and buttons - this will give stylish fonts on textview and button etc
    tb = Typeface.createFromAsset(getAssets(), "fonts/TitilliumWeb-Bold.ttf");
    sb = Typeface.createFromAsset(getAssets(), "fonts/shablagooital.ttf");
    triviaQuizText.setTypeface(sb);
    questionText.setTypeface(tb);
    buttonA.setTypeface(tb);
    buttonB.setTypeface(tb);
    buttonC.setTypeface(tb);
    buttonD.setTypeface(tb);
    timeText.setTypeface(tb);
    resultText.setTypeface(sb);
    coinText.setTypeface(tb);

    //Our database helper class
    triviaQuizHelper = new TriviaQuizHelper(this);
    //Make db writable
    triviaQuizHelper.getWritableDatabase();

    //It will check if the ques,options are already added in table or not
    //If they are not added then the getAllOfTheQuestions() will return a list of size zero
    if (triviaQuizHelper.getAllOfTheQuestions().size() == 0) {
        //If not added then add the ques,options in table
        triviaQuizHelper.allQuestion();
    }

    //This will return us a list of data type TriviaQuestion
    list = triviaQuizHelper.getAllOfTheQuestions();

    //Now we gonna shuffle the elements of the list so that we will get questions randomly
    Collections.shuffle(list);

    //currentQuestion will hold the que, 4 option and ans for particular id
    currentQuestion = list.get(qid);

    //countDownTimer
    countDownTimer = new CountDownTimer(22000, 1000) {
        public void onTick(long millisUntilFinished) {

            //here you can have your logic to set text to timeText
            timeText.setText(String.valueOf(timeValue) + "\"");

            //With each iteration decrement the time by 1 sec
            timeValue -= 1;

            //This means the user is out of time so onFinished will called after this iteration
            if (timeValue == -1) {

                //Since user is out of time setText as time up
                resultText.setText(getString(R.string.timeup));

                //Since user is out of time he won't be able to click any buttons
                //therefore we will disable all four options buttons using this method
                disableButton();
            }
        }

        //Now user is out of time
        public void onFinish() {
            //We will navigate him to the time up activity using below method
            timeUp();
        }
    }.start();

    //This method will set the que and four options
    updateQueAndOptions();


}


public void updateQueAndOptions() {

    //This method will setText for que and options
    questionText.setText(currentQuestion.getQuestion());
    buttonA.setText(currentQuestion.getOptA());
    buttonB.setText(currentQuestion.getOptB());
    buttonC.setText(currentQuestion.getOptC());
    buttonD.setText(currentQuestion.getOptD());


    timeValue = 20;

    //Now since the user has ans correct just reset timer back for another que- by cancel and start
    countDownTimer.cancel();
    countDownTimer.start();

    //set the value of coin text
    coinText.setText(String.valueOf(coinValue));
    //Now since user has ans correct increment the coinvalue
    coinValue++;

}

//Onclick listener for first button
public void buttonA(View view) {
    //compare the option with the ans if yes then make button color green
    if (currentQuestion.getOptA().equals(currentQuestion.getAnswer())) {
        buttonA.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        //Check if user has not exceeds the que limit
        if (qid < list.size() - 1) {

            //Now disable all the option button since user ans is correct so
            //user won't be able to press another option button after pressing one button
            disableButton();

            //Show the dialog that ans is correct
            correctDialog();
        }
        //If user has exceeds the que limit just navigate him to GameWon activity
        else {

            gameWon();

        }
    }
    //User ans is wrong then just navigate him to the PlayAgain activity
    else {

        gameLostPlayAgain();

    }
}

//Onclick listener for sec button
public void buttonB(View view) {
    if (currentQuestion.getOptB().equals(currentQuestion.getAnswer())) {
        buttonB.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        if (qid < list.size() - 1) {
            disableButton();
            correctDialog();
        } else {
            gameWon();
        }
    } else {
        gameLostPlayAgain();
    }
}

//Onclick listener for third button
public void buttonC(View view) {
    if (currentQuestion.getOptC().equals(currentQuestion.getAnswer())) {
        buttonC.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        if (qid < list.size() - 1) {
            disableButton();
            correctDialog();
        } else {
            gameWon();
        }
    } else {

        gameLostPlayAgain();
    }
}

//Onclick listener for fourth button
public void buttonD(View view) {
    if (currentQuestion.getOptD().equals(currentQuestion.getAnswer())) {
        buttonD.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.lightGreen));
        if (qid < list.size() - 1) {
            disableButton();
            correctDialog();
        } else {
            gameWon();
        }
    } else {
        gameLostPlayAgain();
    }
}

//This method will navigate from current activity to GameWon
public void gameWon() {
    Intent intent = new Intent(this, GameWon.class);
    startActivity(intent);
    finish();
}

//This method is called when user ans is wrong
//this method will navigate user to the activity PlayAgain
public void gameLostPlayAgain() {
    Intent intent = new Intent(this, PlayAgain.class);
    startActivity(intent);
    finish();
}

//This method is called when time is up
//this method will navigate user to the activity Time_Up
public void timeUp() {
    Intent intent = new Intent(this, Time_Up.class);
    startActivity(intent);
    finish();
}

//If user press home button and come in the game from memory then this
//method will continue the timer from the previous time it left
@Override
protected void onRestart() {
    super.onRestart();
    countDownTimer.start();
}

//When activity is destroyed then this will cancel the timer
@Override
protected void onStop() {
    super.onStop();
    countDownTimer.cancel();
}

//This will pause the time
@Override
protected void onPause() {
    super.onPause();
    countDownTimer.cancel();
}

//On BackPressed
@Override
public void onBackPressed() {
    Intent intent = new Intent(this, HomeScreen.class);
    startActivity(intent);
    finish();
}

//This dialog is show to the user after he ans correct
public void correctDialog() {
    final Dialog dialogCorrect = new Dialog(MainGameActivity.this);
    dialogCorrect.requestWindowFeature(Window.FEATURE_NO_TITLE);
    if (dialogCorrect.getWindow() != null) {
        ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
        dialogCorrect.getWindow().setBackgroundDrawable(colorDrawable);
    }
    dialogCorrect.setContentView(R.layout.dialog_correct);
    dialogCorrect.setCancelable(false);
    dialogCorrect.show();

    //Since the dialog is show to user just pause the timer in background
    onPause();


    TextView correctText = (TextView) dialogCorrect.findViewById(R.id.correctText);
    FButton buttonNext = (FButton) dialogCorrect.findViewById(R.id.dialogNext);

    //Setting type faces
    correctText.setTypeface(sb);
    buttonNext.setTypeface(sb);

    //OnCLick listener to go next que
    buttonNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //This will dismiss the dialog
            dialogCorrect.dismiss();
            //it will increment the question number
            qid++;
            //get the que and 4 option and store in the currentQuestion
            currentQuestion = list.get(qid);
            //Now this method will set the new que and 4 options
            updateQueAndOptions();
            //reset the color of buttons back to white
            resetColor();
            //Enable button - remember we had disable them when user ans was correct in there particular button methods
            enableButton();
        }
    });
}


//This method will make button color white again since our one button color was turned green
public void resetColor() {
    buttonA.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
    buttonB.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
    buttonC.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
    buttonD.setButtonColor(ContextCompat.getColor(getApplicationContext(),R.color.white));
}

//This method will disable all the option button
public void disableButton() {
    buttonA.setEnabled(false);
    buttonB.setEnabled(false);
    buttonC.setEnabled(false);
    buttonD.setEnabled(false);
}

//This method will all enable the option buttons
public void enableButton() {
    buttonA.setEnabled(true);
    buttonB.setEnabled(true);
    buttonC.setEnabled(true);
    buttonD.setEnabled(true);
    }


}

标签: androidsqlite

解决方案


已编辑

只需删除if else所有按钮内的包装更好地保留它,不要重复代码。我假设显示结果的屏幕是在内部处理的,gameWon并且您已经实现了以下功能inCorrectDialog

public void buttonA(View view) {
 Button button = (Button) view;
 buttonPressed(button);
}
public void buttonB(View view) {
 Button button = (Button) view;
 buttonPressed(button);
}

public void buttonC(View view) {
 Button button = (Button) view;
 buttonPressed(button);
}

public void buttonD(View view) {
 Button button = (Button) view;
 buttonPressed(button);
}

public void buttonPressed(Button button) {

 button.setButtonColor(ContextCompat.getColor(getApplicationContext(), R.color.lightGreen));
 if (qid < list.size() - 1) {
  disableButton();
  if (currentQuestion.getOptA().equals(currentQuestion.getAnswer())) {
   correctDialog();
  } else {
   inCorrectDialog();
  }

 } else {
  gameWon();
 }

}

推荐阅读