簡體   English   中英

您好,我正在做一個android測驗,當問題完成但轉到結果活動時,轉到結果活動

[英]Hello, I am making an android quiz to go to the result activity when questions are completed but its not going to result activity

我的項目代碼在此處給出,在此我想在問題結束后轉到結果活動。 僅在時間到了時才進行結果活動,而在問題結束時則沒有結果。 我應該在if(<20)行代碼中實現什么才能轉到結果頁面。

MyQuizActivity:

public class MyQuizActivity extends AppCompatActivity {
    private QuestionBank mQuestionBank = new QuestionBank();

        private TextView mScoreView;
        private TextView times;//For Timer in Quiz
        private TextView mQuestionView;
        private Button mButtonChoice1;
        private Button mButtonChoice2;
        private Button mButtonChoice3;
        private Button mButtonChoice4;


        private String mAnswer;
        private double mScore = 0;
        private int mQuestionNumber = 0;

    private int count_correct_answer =0;
    private int count_wrong_answer=0;


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

    mScoreView = (TextView) findViewById(R.id.score);
    mQuestionView = (TextView) findViewById(R.id.question);
    mButtonChoice1 = (Button) findViewById(R.id.choice1);
    mButtonChoice2 = (Button) findViewById(R.id.choice2);
    mButtonChoice3 = (Button) findViewById(R.id.choice3);
    mButtonChoice4 = (Button) findViewById(R.id.choice4);
    updateQuestion();

    // Set the timer for Quiz
    times = (TextView) findViewById(R.id.timers);
// method which will set the things up for our game
    times.setText("00:02:00");
// A timer of 20 minutes to play for, with an interval of 1 second (1000 milliseconds)
    CounterClass timer = new CounterClass(1200000, 1000);
    timer.start();
    //Code Ends here

    if (mQuestionNumber< 20) {
// if questions are not over then do this

                mQuestionView.setText(mQuestionBank.getQuestion(mQuestionNumber));

                //set all buttons Blue for next question
                mButtonChoice1.setBackgroundColor(Color.BLUE);
                mButtonChoice2.setBackgroundColor(Color.BLUE);
                mButtonChoice3.setBackgroundColor(Color.BLUE);
                mButtonChoice4.setBackgroundColor(Color.BLUE);
                updateQuestion();

    }
    else {
// if over do this
        Intent intent = new Intent(MyQuizActivity.this,
                ResultActivity.class);
        Bundle b = new Bundle();
        b.putDouble("mScore", mScore); // Your score
        intent.putExtras(b); // Put your score to your next
        startActivity(intent);
        finish();
    }


    //Start of Button Listener for Button1
    mButtonChoice1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //My Logic for Button goes in here
            if (mButtonChoice1.getText() == mAnswer) {
                //Post Question Delay Code
                final Handler handle = new Handler();
                Runnable delay = new Runnable() {
                    public void run() {
                        //Code Ends hers
                        mScore = mScore + 02.00;
                        count_correct_answer++;
                        updateScore(mScore);
                        updateQuestion();
                    }
                };
                handle.postDelayed(delay, 2000);
                mButtonChoice1.setBackgroundColor(Color.GREEN);
                //This line of code is optional
                Toast.makeText(MyQuizActivity.this, "Correct Answer", Toast.LENGTH_SHORT).show();
            }

            else {
                //Post Question Delay Code
                final Handler handle = new Handler();
                Runnable delay = new Runnable() {
                    public void run() {
                        //Code Ends hers
                        mScore = (mScore - 00.66);
                        count_wrong_answer++;
                        updateScore(mScore);
                        updateQuestion();
                    }
                };
                handle.postDelayed(delay, 2000);
                mButtonChoice1.setBackgroundColor(Color.RED);
                //This line of code is optional
                Toast.makeText(MyQuizActivity.this, "Wrong Answer", Toast.LENGTH_SHORT).show();
            }
        }
    });
    //End of Button Listener for Button1

    //Start of Button Listener for Button2
    mButtonChoice2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //My Logic for Button goes in here
            if (mButtonChoice2.getText() == mAnswer) {
                //Post Question Delay Code
                final Handler handle = new Handler();
                Runnable delay = new Runnable() {
                    public void run() {
                        //Code Ends hers
                        mScore = mScore + 02.00;
                        count_correct_answer++;
                        updateScore(mScore);
                        updateQuestion();
                    }
                };
                handle.postDelayed(delay, 2000);
                mButtonChoice2.setBackgroundColor(Color.GREEN);
                //This line of code is optional
                Toast.makeText(MyQuizActivity.this, "Correct Answer", Toast.LENGTH_SHORT).show();
            }

            else {
                //Post Question Delay Code
                final Handler handle = new Handler();
                Runnable delay = new Runnable() {
                    public void run() {
                        //Code Ends hers
                        mScore = (mScore - 00.66);
                        count_wrong_answer++;
                        updateScore(mScore);
                        updateQuestion();
                    }
                };
                handle.postDelayed(delay, 2000);
                mButtonChoice2.setBackgroundColor(Color.RED);
                //This line of code is optional
                Toast.makeText(MyQuizActivity.this, "Wrong Answer", Toast.LENGTH_SHORT).show();
            }
        }
    });
    //End of Button Listener for Button2

    //Start of Button Listener for Button3
    mButtonChoice3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //My Logic for Button goes in here
            if (mButtonChoice3.getText() == mAnswer) {
                //Post Question Delay Code
                final Handler handle = new Handler();
                Runnable delay = new Runnable() {
                    public void run() {
                        //Code Ends hers
                        mScore = mScore + 02.00;
                        count_correct_answer++;
                        updateScore(mScore);
                        updateQuestion();
                    }
                };
                handle.postDelayed(delay, 2000);
                mButtonChoice3.setBackgroundColor(Color.GREEN);
                //This line of code is optional
                Toast.makeText(MyQuizActivity.this, "Correct Answer", Toast.LENGTH_SHORT).show();
            }

            else {
                //Post Question Delay Code
                final Handler handle = new Handler();
                Runnable delay = new Runnable() {
                    public void run() {
                        //Code Ends hers
                        mScore = (mScore - 00.66);
                        count_wrong_answer++;
                        updateScore(mScore);
                        updateQuestion();
                    }
                };
                handle.postDelayed(delay, 2000);
                mButtonChoice3.setBackgroundColor(Color.RED);
                //This line of code is optional
                Toast.makeText(MyQuizActivity.this, "Wrong Answer", Toast.LENGTH_SHORT).show();
            }
        }
    });
    //End of Button Listener for Button3

    //Start of Button Listener for Button4
    mButtonChoice4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //My Logic for Button goes in here
            if (mButtonChoice4.getText() == mAnswer) {
                //Post Question Delay Code
                final Handler handle = new Handler();
                Runnable delay = new Runnable() {
                    public void run() {
                        //Code Ends hers
                        mScore = mScore + 02.00;
                        count_correct_answer++;
                        updateScore(mScore);
                        updateQuestion();
                    }
                };
                handle.postDelayed(delay, 2000);
                mButtonChoice4.setBackgroundColor(Color.GREEN);
                //This line of code is optional
                Toast.makeText(MyQuizActivity.this, "Correct Answer", Toast.LENGTH_SHORT).show();
            }

            else {
                //Post Question Delay Code
                final Handler handle = new Handler();
                Runnable delay = new Runnable() {
                    public void run() {
                        //Code Ends hers
                        mScore = (mScore - 00.66);
                        count_wrong_answer++;
                        updateScore(mScore);
                        updateQuestion();
                    }
                };
                handle.postDelayed(delay, 2000);
                mButtonChoice4.setBackgroundColor(Color.RED);
                //This line of code is optional
                Toast.makeText(MyQuizActivity.this, "Wrong Answer", Toast.LENGTH_SHORT).show();
            }
        }
    });
    //End of Button Listener for Button4
}

    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @SuppressLint("NewApi")
    public class CounterClass extends CountDownTimer {
        public CounterClass(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
        }
        @Override
        public void onFinish() {
            times.setText("Time is up");
            Intent intent = new Intent(MyQuizActivity.this,
                    ResultActivity.class);

            Bundle b = new Bundle();
            b.putDouble("mScore", + mScore); // Your score
            intent.putExtras(b); // Put your score to your next
            startActivity(intent);

            Bundle c = new Bundle();
            c.putInt("count_correct_answer", + count_correct_answer); // Your score
            intent.putExtras(c); // Put your Correct Answer score to your next
            startActivity(intent);

            Bundle d = new Bundle();
            d.putInt("count_wrong_answer", + count_wrong_answer); // Your score
            intent.putExtras(d); // Put your Wrong Answer score to your next
            startActivity(intent);

            finish();
        }
        @Override
        public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
            long millis = millisUntilFinished;
            String hms = String.format(
                    "%02d:%02d:%02d",
                    TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis)
                            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                            .toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis)
                            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                            .toMinutes(millis)));
            System.out.println(hms);
            times.setText(hms);
        }
    }



        private void updateQuestion(){
            mQuestionView.setText(mQuestionBank.getQuestion(mQuestionNumber));
            mButtonChoice1.setText(mQuestionBank.getChoice1(mQuestionNumber));
            mButtonChoice2.setText(mQuestionBank.getChoice2(mQuestionNumber));
            mButtonChoice3.setText(mQuestionBank.getChoice3(mQuestionNumber));
            mButtonChoice4.setText(mQuestionBank.getChoice4(mQuestionNumber));

            mAnswer = mQuestionBank.getCorrectAnswer(mQuestionNumber);
            mQuestionNumber++;
        }
        private void updateScore(double point){
            mScoreView.setText("" + mScore);
        }

    }

ResultActivity:

public class ResultActivity extends AppCompatActivity {

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

        //Showing Test Result
        TextView textResult = (TextView) findViewById(R.id.textResult);
        Bundle b = getIntent().getExtras();
        Double mScore = b.getDouble("mScore", 0.0);
        textResult.setText("Your Score is :  " + mScore + " .   Thanks for playing my game.");
        //Code Ends here

        //Showing Correct Answers
        TextView correctanswer = (TextView) findViewById(R.id.correctanswer);
        Bundle c = getIntent().getExtras();
        int count_correct_answer = c.getInt("count_correct_answer", 0 );
        correctanswer.setText("No. of Correct answers are :  " + count_correct_answer + " . ");
        //Code Ends Here

        //Showing Wrong Answers
        TextView wronganswer = (TextView) findViewById(R.id.wronganswer);
        Bundle d = getIntent().getExtras();
        int count_wrong_answer = d.getInt("count_wrong_answer", 0 );
        wronganswer.setText("No. of Wrong answers are :  " + count_wrong_answer + " . ");
        //Code Ends Here

    }

    public void playagain(View o) {
        Intent intent = new Intent(this, MyQuizActivity.class);
        startActivity(intent);
    }

您添加了多個捆綁,所以我認為它不起作用。

 Intent intent = new Intent(MyQuizActivity.this,
                    ResultActivity.class);

            intent.putExtra("mScore", +mScore); //put score
            intent.putExtras("count_correct_answer", +count_correct_answer); // Put your Correct Answer score to your next
            intent.putExtras("count_wrong_answer", +count_wrong_answer); // Put your Wrong Answer score to your next
            startActivity(intent);

            finish();

僅在時間到了時才進行結果活動,而在問題結束時則沒有結果。

這是因為在活動開始時onCreate()僅被調用一次,因此此代碼

if (mQuestionNumber< 20) {
         mQuestionView.setText(mQuestionBank.getQuestion(mQuestionNumber));

            //set all buttons Blue for next question
            mButtonChoice1.setBackgroundColor(Color.BLUE);
            mButtonChoice2.setBackgroundColor(Color.BLUE);
            mButtonChoice3.setBackgroundColor(Color.BLUE);
            mButtonChoice4.setBackgroundColor(Color.BLUE);
            updateQuestion();

}
else {
    Intent intent = new Intent(MyQuizActivity.this,
            ResultActivity.class);
    Bundle b = new Bundle();
    b.putDouble("mScore", mScore); // Your score
    intent.putExtras(b); // Put your score to your next
    startActivity(intent);
    finish();
}

僅在活動開始時運行,然后您才更新問題和選擇,否則其他部分實際上並未運行。

因此,您可以做的是檢查updateQuestion()中的問題計數並從那里調用結果活動。

在開始之前使用finish() - startactivity(your intent) ..您的當前條件在啟動活動后完成了...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM