如何在Android Studio中正确使用postDelayed()? [英] how to use postDelayed() correctly in android studio?

查看:478
本文介绍了如何在Android Studio中正确使用postDelayed()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个countDownTimer,如果用户在12秒内未按下gameButton,我希望调用gameOver方法.问题我要么在countDownTimer为12时获得即时调用的游戏函数,要么计时器一直在递减计数.因此,我尝试使用postDelayed()方法给用户一秒钟的时间来按下按钮,并让countDownTimer继续,但是由于我的代码是正确的,所以无论如何游戏都停止在12点.

I have a countDownTimer and if the user does not hit the gameButton within the 12th second I want the gameOver method called. problem I either get game function called instantly when countDownTimer is 12 or the timer just keeps counting down. So I am trying to use the postDelayed() method to give the user a full second to hit the button and let the countDownTimer to continue but as my code is right now the game stops on 12 regardless.

import android.app.Activity;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;



public class GameScreen extends Activity {

    private TextView time;
    private Button start;
    private Button cancel;
    private Button gameButton;
    private CountDownTimer countDownTimer;
    public static int count = 0;
    public static int countFail = 0;

    final Handler handler = new Handler();
    final Runnable r = new Runnable() {
        public void run() {
            handler.postDelayed(this, 1000);
            gameOver();
        }
    };


    private View.OnClickListener btnClickListener = new View.OnClickListener(){

        @Override
        public void onClick(View v) {

            switch(v.getId()){
                case R.id.start_ID :
                    start();
                    break;
                case R.id.cancel :
                    cancel();
                    break;
                case R.id.gameButton_ID :
                    gameButton();
                    break;
            }

        }


    };


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


        start = (Button) findViewById(R.id.start_ID);
        start.setOnClickListener(btnClickListener);
        cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(btnClickListener);
        time = (TextView) findViewById(R.id.time);
        gameButton = (Button) findViewById(R.id.gameButton_ID);
        gameButton.setOnClickListener(btnClickListener);


    }

    public void start(){

        time.setText("16");
        //This doesn't work and makes app crash when you hit start button

        countDownTimer = new CountDownTimer(16 * 1000, 1000) {
            @Override
            public void onTick(long millsUntilFinished){
                time.setText("" + millsUntilFinished / 1000);

                //turns textview string to int
                int foo = Integer.parseInt(time.getText().toString());

                if(time.getText().equals("12")){

                    r.run();

                }

            }

            public void onFinish() {
                time.setText("Done !");
            }
        };
        countDownTimer.start();
    }

    private void cancel(){
        if(countDownTimer != null){
            countDownTimer.cancel();
            countDownTimer = null;
        }
    }

    private void gameOver(){
        Toast.makeText(getApplicationContext(), "You scored " + count, Toast.LENGTH_SHORT).show();
        count = 0;
        countFail = 0;
        cancel();
    }

    private void gameButton(){

        int foo = Integer.parseInt(time.getText().toString());

        if(foo  % 2 == 0 ) {
            Toast.makeText(getApplicationContext(), "PASS", Toast.LENGTH_SHORT).show();
            handler.removeCallbacks(r);
            ++count;
        }

        else{
            gameOver();
        }
    }

}

推荐答案

您几乎可以正确使用postDelayed(Runnable, long),但不能完全使用.让我们来看看您的Runnable.

You're almost using postDelayed(Runnable, long) correctly, but just not quite. Let's take a look at your Runnable.

final Runnable r = new Runnable() {
    public void run() {
        handler.postDelayed(this, 1000);
        gameOver();
    }
};

当我们调用r.run();时,首先要做的是告诉您handler在1000毫秒后运行完全相同的Runnable,然后调用gameOver().实际上,这将导致您的gameOver()方法被调用两次:一次,一次,一次,一次,处理程序等待1000毫秒.

When we call r.run(); the first thing it's going to do is tell your handler to run the very same Runnable after 1000 milliseconds, and then to call gameOver(). What this will actually result in is your gameOver() method being called twice: once right away, and a second time once the Handler is done waiting 1000 milliseconds.

相反,您应该将Runnable更改为此:

Instead, you should change your Runnable to this:

final Runnable r = new Runnable() {
    public void run() {
        gameOver();
    }
};

并这样称呼它:

handler.postDelayed(r, 1000);

希望这会有所帮助.

这篇关于如何在Android Studio中正确使用postDelayed()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆