有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java问答应用程序在最后一个问题中未显示对话框

因此,当在测验中问最后一个问题时,如果正确与否,它不会显示对话框。在结果屏幕上,最后一个问题始终被认为是“正确的”

public class MainGameActivity extends AppCompatActivity {
    FButton buttonA, buttonB, buttonC, buttonD;
    Button pause;
    TextView questionText, triviaQuizText, timeText, resultText, coinText;
    TriviaQuizHelper triviaQuizHelper;
    TriviaQuestion currentQuestion;
    MediaPlayer mysong;

    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);
        mysong = MediaPlayer.create(MainGameActivity.this,R.raw.hs);
        mysong.start();
        int media_length;

        //Initializing variables
        questionText = (TextView) findViewById(R.id.triviaQuestion);
        pause = (Button) findViewById(R.id.pause);
        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);

        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pausedialog();
            }
        });





        //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));
                    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++;

    }

    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) {


        if (qid < list.size() - 1) {

            disableButton();
            if (button.getText().equals(currentQuestion.getAnswer())) {
                correctDialog();
            } else {
                incorrectDialog();
                coinValue--;

            }

        } else {

            gameWon();
        }

    }









    //This method will navigate from current activity to GameWon
    public void gameWon() {

        Intent intent = new Intent(this, GameWon.class);

        intent.putExtra("score", coinValue);

        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();
        mysong.getCurrentPosition();
mysong.start();
        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();

        mysong.pause();
        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();
        onRestart();




        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();
            }
        });
    }


    //INCORRECT//////////////////////////////////////////////////////////////
    public void incorrectDialog() {
        final Dialog incorrectDialog = new Dialog(MainGameActivity.this);
        incorrectDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (incorrectDialog.getWindow() != null) {
            ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
            incorrectDialog.getWindow().setBackgroundDrawable(colorDrawable);
        }
        incorrectDialog.setContentView(R.layout.dialog_incorrect);
        incorrectDialog.setCancelable(false);
        incorrectDialog.show();

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




        TextView IncorrectText = (TextView) incorrectDialog.findViewById(R.id.IncorrectText);
        FButton buttonNext = (FButton) incorrectDialog.findViewById(R.id.dialogNext);

        //Setting type faces
        IncorrectText.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
                incorrectDialog.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);
    }


    public void pausedialog()
    {

        final Dialog pausedialog = new Dialog(MainGameActivity.this);
        pausedialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (pausedialog.getWindow() != null) {
            ColorDrawable colorDrawable = new ColorDrawable(Color.TRANSPARENT);
            pausedialog.getWindow().setBackgroundDrawable(colorDrawable);
        }
        pausedialog.setContentView(R.layout.activity_dialog_pause);
        pausedialog.setCancelable(false);
        pausedialog.show();



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

        Button resume = (FButton) pausedialog.findViewById(R.id.resume);
        Button menu = (FButton) pausedialog.findViewById(R.id.menu);
        Button music = (FButton) pausedialog.findViewById(R.id.music);


        resume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //This will dismiss the dialog
                pausedialog.dismiss();
                onRestart();





            }

        });


        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent2 = new Intent(MainGameActivity.this,HomeScreen.class);
                   //  mysong.release();
                    //  finish();
                startActivity(intent2);
                finish();
            }
        });






    }





}


编辑:发布完整代码。我试着再次理解它,但我不知道该怎么做,这可能是我唯一剩下的问题,除了插入图像(这是另一个问题,现在我想完成这个)

编辑:我试着像这样修复if语句:

      if (button.getText().equals(currentQuestion.getAnswer())) {
            correctDialog();
        } else {
            incorrectDialog();
            coinValue--;

        }




        if (qid < list.size() - 1) {

            disableButton();


        } else

            {

            gameWon();
        }

它现在可以工作了,最后一个答案现在也可以注册,如果正确或错误,但是对话框会闪烁一秒钟并自动转到结果屏幕


共 (0) 个答案