【问题标题】:Unable to use Handler properly无法正确使用Handler
【发布时间】:2019-02-11 05:36:33
【问题描述】:

我开始学习 Android 开发。我正在构建一个基本的加法游戏,用户必须单击显示两个数字相加的按钮。有 四个 Textviews。第一个为每个要回答的问题提供time limit。其次为用户提供question。第三个给出用户的current score,最后一个给出选择的打开是正确还是不正确。 除第一个按钮外,一切正常。每次按下按钮时,计数都会非常快。

// When First button is pressed

public void onClickButton1(View view) {
        correctIncorrect.setVisibility(View.VISIBLE);
        if (Integer.parseInt(button1.getText().toString())==sum) {
            correctIncorrect.setText("Correct");
            correctUpdater();
        }
        else {
            correctIncorrect.setText("Incorrect");
            inCorrectUpdater();
        }

}

//Similar to all the buttons


//To Update the score

public void correctUpdater() {
        n++;
        yourScore++;
        score.setText(Integer.toString(yourScore) + "/" + Integer.toString(n));
        update();
}

public void inCorrectUpdater() {
    n++;
    score.setText(Integer.toString(yourScore) + "/" + Integer.toString(n));
    update();
}


// To update the timer

//=======================================================================//
    public void resetTimer() {
        timer.setText(Integer.toString(temp)+"s");
        if (temp == 0) {
            inCorrectUpdater();
            update();
        }
        else {
            timeUpdater();
        }

    }

    public void timeUpdater() {
        Handler timeHandler = new Handler();
        Runnable timeRunnable = new Runnable() {
            @Override
            public void run() {
                temp--;
                resetTimer();
            }
        };
        timeHandler.postDelayed(timeRunnable,1000);
    }
    //=================================================================//

// Updater function

public void update() {

        correctIncorrect.setVisibility(View.INVISIBLE);
        Random random = new Random();
        int a = random.nextInt(21);
        int b = random.nextInt(21);
        question.setText(Integer.toString(a) + " + " + Integer.toString(b));
        Log.i("info", "onCreate: a = " + a);
        Log.i("info", "onCreate: b = " + b);
        sum = a+b;
        Log.i("info", "onCreate: sum = " + sum);
        int whichButton = random.nextInt(4);
        Log.i("info", "onCreate: random button is " + whichButton);
        values.clear();
        for (int i = 0; i< 4; i++) {
            if (i == whichButton) {
                values.add(sum);
            }
            else {
                values.add(random.nextInt(50));
            }
            Log.i("info", "onCreate: value[" + i + "] = " + values.get(i));
        }
        button1.setText(Integer.toString(values.get(0)));
        button2.setText(Integer.toString(values.get(1)));
        button3.setText(Integer.toString(values.get(2)));
        button4.setText(Integer.toString(values.get(3)));
        temp = 10;
        resetTimer();
}

我是否错误地使用了 Handler?我能做什么?

【问题讨论】:

    标签: java android runnable android-handler


    【解决方案1】:

    我是否错误地使用了处理程序?我能做什么?

    一个更好的方法是CountDownTimer,通过使用它你不必自己处理Handler,你也有一个cancel的规定运行倒计时。

    除第一个按钮外,一切正常

    不太确定在 onClick() 中使用相同代码单击按钮时会导致不同行为的原因。但是您可以考虑对所有 4 个按钮使用相同的 onClickListener,这样:

    View.OnClickListener myListener = new View.OnClickListener() {
            @Override
            public void onClick(View button) {
                correctIncorrect.setVisibility(View.VISIBLE);
                if (Integer.parseInt(button.getText().toString())==sum) {
                    correctIncorrect.setText("Correct");
                    correctUpdater();
                }
                else {
                    correctIncorrect.setText("Incorrect");
                    inCorrectUpdater();
                }
            }
        };
    
    button1.setOnClickListener(myListener);
    button2.setOnClickListener(myListener);
    button3.setOnClickListener(myListener);
    button4.setOnClickListener(myListener);
    

    这将确保单击 4 个按钮中的任何一个都具有相同的行为(当然取决于答案)

    如果您对 CountDownTimer 上的 onTick() 方法感到困惑,可以使用modified CountDownTimer,这将确保计时器不会错过任何滴答声。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      相关资源
      最近更新 更多