我在哪里可以将viewGame()方法放入OnClick()Answers方法中

时间:2019-03-01 19:59:41

标签: android

我正在使用android studio开发一个测验应用程序来玩测验,当我单击按钮中的答案时,它将暂停游戏并显示一个描述问题的完整描述并具有按钮名称恢复的布局。点击简历后,它将返回并继续游戏并显示下一个问题

这是Playing.class的代码 这是我在viewGame方法上的代码

 private void viewGame(View view) {
        if (pause_flg == false) {
            pause_flg = true;

            mCountDown.cancel();
            mCountDown = null;
            frameview.setVisibility(View.VISIBLE);

        }else{
            pause_flg = false;
            resume.setBackgroundColor(Color.GREEN);
            frameview.setVisibility(View.GONE);
            mCountDown = new CountDownTimer(mTimeLeftInMillis, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    mTimeLeftInMillis = millisUntilFinished;
                    int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
                    int seconds = (int) (mTimeLeftInMillis / 1000) % 60;

                    String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);

                    txtTimer.setText(timeLeftFormatted);


                }

                @Override
                public void onFinish() {
                    //when time stop it will go to the next question
                    Intent myIntent = new Intent(Playing.this,Done.class);
                    showQuestion(index);
                    finish();
                }
            };
            showQuestion(index);
        }

    }

这是我在OnClick Answers方法上的代码

  public void onClick(View view) {

        if (index < totalQuestion) //still have question in list
        {

            btnA.setOnClickListener((View v) -> {

                if(btnA.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer()))
                {

                    btnA.setBackgroundColor(Color.GREEN);
                    btnA.setEnabled(false);
                    btnB.setEnabled(false);
                    btnC.setEnabled(false);
                    btnD.setEnabled(false);
                    Handler handler = new Handler();
                    handler.postDelayed(() -> {


                        btnA.setBackgroundColor(Color.parseColor("#03A9F4"));
                        score += 1;
                        correctAnswer++;
                        showQuestion(++index);
                        btnA.setEnabled(true);
                        btnB.setEnabled(true);
                        btnC.setEnabled(true);
                        btnD.setEnabled(true);
                        txtScore.setText(String.format("%d", score));
                    },1500);
                }
                else {

                   // btnA.setBackgroundColor(Color.RED);
                    btnA.setEnabled(false);
                    btnB.setEnabled(false);
                    btnC.setEnabled(false);
                    btnD.setEnabled(false);
                    if(btnB.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer()))
                    {
                        btnB.setBackgroundColor(Color.GREEN);
                    }
                    else if(btnC.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer()))
                    {
                        btnC.setBackgroundColor(Color.GREEN);
                    }
                    else if(btnD.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer()))
                    {
                        btnD.setBackgroundColor(Color.GREEN);
                    }
                    Handler handler = new Handler();
                    handler.postDelayed(() -> {

                        btnA.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnB.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnC.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnD.setBackgroundColor(Color.parseColor("#03A9F4"));
                        showQuestion(++index);
                        btnA.setEnabled(true);
                        btnB.setEnabled(true);
                        btnC.setEnabled(true);
                        btnD.setEnabled(true);
                    }, 1500);


                }

            });
            btnB.setOnClickListener((View v) -> {


                if (btnB.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {

                    //btnB.setBackgroundColor(Color.GREEN);
                    btnA.setEnabled(false);
                    btnB.setEnabled(false);
                    btnC.setEnabled(false);
                    btnD.setEnabled(false);
                    Handler handler = new Handler();
                    handler.postDelayed(() -> {


                        btnA.setEnabled(true);
                        btnB.setEnabled(true);
                        btnC.setEnabled(true);
                        btnD.setEnabled(true);
                        btnB.setBackgroundColor(Color.parseColor("#03A9F4"));
                        score += 1;
                        correctAnswer++;
                        showQuestion(++index);
                        txtScore.setText(String.format("%d", score));
                    }, 1500);
                } else {

                    btnB.setBackgroundColor(Color.RED);
                    btnA.setEnabled(false);
                    btnB.setEnabled(false);
                    btnC.setEnabled(false);
                    btnD.setEnabled(false);
                    if (btnA.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {
                        btnA.setBackgroundColor(Color.GREEN);
                    } else if (btnC.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {
                        btnC.setBackgroundColor(Color.GREEN);
                    } else if (btnD.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {
                        btnD.setBackgroundColor(Color.GREEN);
                    }
                    Handler handler = new Handler();
                    handler.postDelayed(() -> {

                        btnA.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnB.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnC.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnD.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnA.setEnabled(true);
                        btnB.setEnabled(true);
                        btnC.setEnabled(true);
                        btnD.setEnabled(true);
                        showQuestion(++index);
                    }, 1500);


                }

            });
            btnC.setOnClickListener((View v) -> {

                if (btnC.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {

                    btnC.setBackgroundColor(Color.GREEN);
                    btnA.setEnabled(false);
                    btnB.setEnabled(false);
                    btnC.setEnabled(false);
                    btnD.setEnabled(false);
                    Handler handler = new Handler();
                    handler.postDelayed(() -> {

                        btnA.setEnabled(true);
                        btnB.setEnabled(true);
                        btnC.setEnabled(true);
                        btnD.setEnabled(true);
                        btnC.setBackgroundColor(Color.parseColor("#03A9F4"));
                        score += 1;
                        correctAnswer++;
                        showQuestion(++index);
                        txtScore.setText(String.format("%d", score));
                    }, 1500);
                } else {

                    btnC.setBackgroundColor(Color.RED);
                    btnA.setEnabled(false);
                    btnB.setEnabled(false);
                    btnC.setEnabled(false);
                    btnD.setEnabled(false);
                    if (btnA.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {
                        btnA.setBackgroundColor(Color.GREEN);
                    } else if (btnB.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {
                        btnB.setBackgroundColor(Color.GREEN);
                    } else if (btnD.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {
                        btnD.setBackgroundColor(Color.GREEN);
                    }
                    Handler handler = new Handler();
                    handler.postDelayed(() -> {

                        btnA.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnB.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnC.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnD.setBackgroundColor(Color.parseColor("#03A9F4"));
                        btnA.setEnabled(true);
                        btnB.setEnabled(true);
                        btnC.setEnabled(true);
                        btnD.setEnabled(true);
                        showQuestion(++index);
                    }, 1500);


                }

            });
            btnD.setOnClickListener((View v) -> {
                {
                    if (btnD.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {

                        btnD.setBackgroundColor(Color.GREEN);
                        btnA.setEnabled(false);
                        btnB.setEnabled(false);
                        btnC.setEnabled(false);
                        btnD.setEnabled(false);
                        Handler handler = new Handler();
                        handler.postDelayed(() -> {


                            btnA.setEnabled(true);
                            btnB.setEnabled(true);
                            btnC.setEnabled(true);
                            btnD.setEnabled(true);
                            btnD.setBackgroundColor(Color.parseColor("#03A9F4"));
                            score += 1;
                            correctAnswer++;
                            showQuestion(++index);
                            txtScore.setText(String.format("%d", score));
                        }, 1500);
                    } else {

                        btnD.setBackgroundColor(Color.RED);
                        btnA.setEnabled(false);
                        btnB.setEnabled(false);
                        btnC.setEnabled(false);
                        btnD.setEnabled(false);
                        if (btnA.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {
                            btnA.setBackgroundColor(Color.GREEN);
                        } else if (btnB.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {
                            btnB.setBackgroundColor(Color.GREEN);
                        } else if (btnC.getText().toString().equals(Common.questionList.get(index).getCorrectAnswer())) {
                            btnC.setBackgroundColor(Color.GREEN);
                        }
                        Handler handler = new Handler();
                        handler.postDelayed(() -> {

                            btnA.setBackgroundColor(Color.parseColor("#03A9F4"));
                            btnB.setBackgroundColor(Color.parseColor("#03A9F4"));
                            btnC.setBackgroundColor(Color.parseColor("#03A9F4"));
                            btnD.setBackgroundColor(Color.parseColor("#03A9F4"));
                            btnA.setEnabled(true);
                            btnB.setEnabled(true);
                            btnC.setEnabled(true);
                            btnD.setEnabled(true);
                            showQuestion(++index);
                        }, 1500);


                    }
                }
            });
            //choose correct Answer
            // next question
            }
            else {
                if (index < totalQuestion) //still have question in list
                {


                    showQuestion(++index); // next question
                }else{
                    //Choose Wrong Answer
                    Intent intent = new Intent(this, Done.class);
                    Bundle dataSend = new Bundle();
                    dataSend.putInt("SCORE", score);
                    dataSend.putInt("TOTAL", totalQuestion);
                    dataSend.putInt("CORRECT", correctAnswer);
                    intent.putExtras(dataSend);
                    startActivity(intent);
                    finish();
                }
            }
        txtScore.setText(String.format("%d", score));
        }

这是我在showQuestion方法中的代码

    private void showQuestion(final int index) {
        if (index < totalQuestion) {
            thisQuestion++;
            txtQuestionNum.setText(String.format("%d / %d", thisQuestion, totalQuestion));
            progressBar.setProgress(100);
            progressValue = 0;


            question_text.setText(Common.questionList.get(index).getQuestion());
            //if question is text we will set image to invisible

            btnA.setText(Common.questionList.get(index).getAnswerA());
            btnB.setText(Common.questionList.get(index).getAnswerB());
            btnC.setText(Common.questionList.get(index).getAnswerC());
            btnD.setText(Common.questionList.get(index).getAnswerD());



            mCountDown.start();


        }
        else
        {

            // if it is final question
            Intent intent = new Intent(Playing.this, Done.class);
            Bundle dataSend = new Bundle();
            dataSend.putInt("SCORE", score);
            dataSend.putInt("TOTAL", totalQuestion);
            dataSend.putInt("CORRECT", correctAnswer);
            intent.putExtras(dataSend);
            startActivity(intent);
            finish();
        }

    }

This is my onResume method or countdowntimer

       protected void onResume(int seconds, final TextView tv) {
        super.onResume();
        totalQuestion = Common.questionList.size();
        mCountDown = new CountDownTimer(mTimeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mTimeLeftInMillis = millisUntilFinished;
                updateCountDownText();


            }

            @Override
            public void onFinish() {
                //when time stop it will go to the next question
                showQuestion(++index);
                Intent myIntent = new Intent(Playing.this,Done.class);
                tv.setText("Completed");
            }
        };
        showQuestion(index);
    }

我在play_activity中的布局

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/duterte_recovered"
    tools:context=".Playing">


    <TextView
        android:id="@+id/question_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:fontFamily="serif"
        android:gravity="center"
        android:textColor="@android:color/black"
        android:textSize="32sp"
        android:textStyle="bold"
        android:typeface="serif"
        android:visibility="visible" />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_margin="30dp"
        android:paddingTop="20dp">

        <LinearLayout
            android:id="@+id/group"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="2">

                <TextView
                    android:id="@+id/txtTotalQuestion"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:text="1/30"
                    android:textColor="@android:color/white"
                    android:textSize="36sp"
                    android:typeface="serif"
                    android:visibility="invisible" />

            </LinearLayout>

            <ProgressBar
                android:id="@+id/progressBar"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:max="20" />

            <Button
                android:id="@+id/btnAnswerA"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@android:color/holo_blue_dark"
                android:foreground="?android:attr/selectableItemBackground"
                android:text="Answer A"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:theme="@style/MyButton"
                android:typeface="serif" />

            <Button
                android:id="@+id/btnAnswerB"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@android:color/holo_blue_dark"
                android:foreground="?android:attr/selectableItemBackground"
                android:text="Answer B"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:theme="@style/MyButton"
                android:typeface="serif" />

            <Button
                android:id="@+id/btnAnswerC"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@android:color/holo_blue_dark"
                android:foreground="?android:attr/selectableItemBackground"
                android:text="Answer C"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:theme="@style/MyButton"
                android:typeface="serif" />

            <Button
                android:id="@+id/btnAnswerD"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@android:color/holo_blue_dark"
                android:foreground="?android:attr/selectableItemBackground"
                android:text="Answer D"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:theme="@style/MyButton"
                android:typeface="serif" />
        </LinearLayout>
    </RelativeLayout>

    <TextView
        android:id="@+id/txtTimer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="28dp"
        android:text="00:00"
        android:textAlignment="center"
        android:textSize="28sp" />

    <TextView
        android:id="@+id/txtScore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="250dp"
        android:paddingLeft="20dp"
        android:text="0"
        android:textAlignment="center"
        android:textSize="36sp" />

    <TextView
        android:id="@+id/txtTimer2"
        android:layout_width="wrap_content"
        android:layout_height="52dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="295dp"
        android:paddingLeft="30dp"
        android:text="Score:"
        android:textAlignment="center"
        android:textSize="22sp" />

    <ImageButton
        android:id="@+id/pauseBtn"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/ic_action_pause"
        android:onClick="pauseGame" />

    <FrameLayout
        android:id="@+id/frame_Lb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="invisible">

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/btn_login">

            <ImageView
                android:layout_width="250dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="60dp"
                android:layout_marginBottom="300dp"
                android:background="@drawable/paus" />

            <TextView
                android:id="@+id/start_lb"
                android:layout_width="400dp"
                android:layout_height="70dp"
                android:layout_gravity="center"
                android:layout_marginBottom="80dp"
                android:text="Tap to Resume"
                android:textAlignment="center"
                android:textSize="30sp"
                android:visibility="invisible" />

            <Button
                android:id="@+id/Exit"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="250dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="60dp"
                android:layout_marginBottom="120dp"
                android:background="@android:color/holo_blue_dark"
                android:text="Exit"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:theme="@style/MyButton"
                android:typeface="serif" />
        </FrameLayout>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/frameview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="invisible">

        <FrameLayout
            android:id="@+id/frame1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/btn_login">


            <TextView
                android:id="@+id/questionview"
                android:layout_width="match_parent"
                android:layout_height="77dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="60dp"
                android:fontFamily="serif"
                android:gravity="center"
                android:textColor="@android:color/black"
                android:textSize="32sp"
                android:textStyle="bold"
                android:typeface="serif"
                android:visibility="visible" />

            <Button
                android:id="@+id/resume"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="250dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="60dp"
                android:layout_marginBottom="120dp"
                android:background="@android:color/holo_blue_dark"
                android:text="Resume"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:theme="@style/MyButton"
                android:typeface="serif" />
        </FrameLayout>
    </FrameLayout>

</RelativeLayout>

0 个答案:

没有答案