在androidstudiojava中执行代码之前,如何设置延迟

brgchamk  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(1089)

我正在创建一个simom说的游戏,如何在代码执行之前设置一个延迟。在图片1中,我需要它等待1秒钟,然后再自动按下按钮。
以下是完整代码:

public class GameActivity extends AppCompatActivity implements View.OnClickListener {
    Random rand = new Random();
    Handler handler = new Handler(Looper.getMainLooper());

    Button btnGreen, btnRed, btnYellow, btnBlue;
    TextView tvScore;

    MediaPlayer mpSound1, mpSound2, mpSound3, mpSound4;
    Animation animation;

    boolean gameOver = false;
    boolean buttonClicked = false;

    String[] fourColors = {"Green", "Red", "Blue", "Yellow"};
    ArrayList<String> allColors = new ArrayList<>();

    int count = 0;
    int scoreToWin = 8;
    int i = 0;

    public void ClickGreen(){
        btnGreen.startAnimation(animation);
        mpSound1.start();
    }
    public void ClickRed(){
        btnRed.startAnimation(animation);
        mpSound2.start();
    }
    public void ClickBlue(){
        btnBlue.startAnimation(animation);
        mpSound3.start();
    }
    public void ClickYellow(){
        btnYellow.startAnimation(animation);
        mpSound4.start();

    }

    public void game(){
        count = 0;
        i = 0;
        int random;
        random = rand.nextInt(4);
        allColors.add(fourColors[random]);
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                for(i = 0; i < allColors.size(); i++) {
                    switch (allColors.get(i)) {
                        case "Green":
                            ClickGreen();
                            Toast.makeText(GameActivity.this, "Green", Toast.LENGTH_SHORT).show();
                            break;
                        case "Red":
                            ClickRed();
                            Toast.makeText(GameActivity.this, "Red", Toast.LENGTH_SHORT).show();
                            break;
                        case "Blue":
                            ClickBlue();
                            Toast.makeText(GameActivity.this, "Blue", Toast.LENGTH_SHORT).show();
                            break;
                        case "Yellow":
                            ClickYellow();
                            Toast.makeText(GameActivity.this, "Yellow", Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            }
        }, 1000);
    }

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

        animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.push_down);

        tvScore = findViewById(R.id.tvScore);

        btnGreen = findViewById(R.id.btnGreen);//0
        btnRed = findViewById(R.id.btnRed);//1
        btnBlue = findViewById(R.id.btnBlue);//2
        btnYellow = findViewById(R.id.btnYellow);//3

        btnGreen.setOnClickListener(this);
        btnRed.setOnClickListener(this);
        btnBlue.setOnClickListener(this);
        btnYellow.setOnClickListener(this);

        mpSound1 = MediaPlayer.create(this, R.raw.sound1);
        mpSound2 = MediaPlayer.create(this, R.raw.sound2);
        mpSound3 = MediaPlayer.create(this, R.raw.sound3);
        mpSound4 = MediaPlayer.create(this, R.raw.sound4);

        game();
    }

    @Override
    public void onClick(View v) {
        if(v.equals(btnGreen)) {
            btnGreen.startAnimation(animation);
            mpSound1.start();
            gameOver = !allColors.get(count).equals("Green");
            count++;
            buttonClicked = true;
        }

        else if(v.equals(btnRed)) {
            btnRed.startAnimation(animation);
            mpSound2.start();
            gameOver = !allColors.get(count).equals("Red");
            count++;
            buttonClicked = true;
        }

        else if(v.equals(btnBlue)) {
            btnBlue.startAnimation(animation);
            mpSound3.start();
            gameOver = !allColors.get(count).equals("Blue");
            count++;
            buttonClicked = true;
        }

        else if(v.equals(btnYellow)) {
            btnYellow.startAnimation(animation);
            mpSound4.start();
            gameOver = !allColors.get(count).equals("Yellow");
            count++;
            buttonClicked = true;
        }

        if(buttonClicked){
            if (gameOver) {
                Intent intent = new Intent(GameActivity.this, GameOverActivity.class);
                startActivity(intent);
            } else {
                if (count >= allColors.size()) {
                    tvScore.setText(String.valueOf(allColors.size()));
                    game();
                    buttonClicked = false;
                    if(allColors.size() == scoreToWin){
                        Toast.makeText(GameActivity.this, "WON!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    }
}

图1图2图3图4

8ehkhllq

8ehkhllq1#

您可以添加如下延迟,以等待1秒: delay(1000)

pu3pd22g

pu3pd22g2#

使用下面的代码在android应用程序中添加延迟。

import android.os.Handler;

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        // Do something after 5s = 5000ms
        // Add your function here which will be executed after 1 second
    }
}, 1000);

相关问题