我怎样才能以特定的方式重复这个倒数计时器......? [英] How can I repeat this countdown timer in a specific way...?

查看:27
本文介绍了我怎样才能以特定的方式重复这个倒数计时器......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

游戏中输出一个随机数(loadG1)并显示4秒.四秒过后,它就会消失,用户必须输入它的值才能获得一分.一旦用户按下回车键,他们的输入框就会消失,程序必须等到更长的 CountDownTimer(当前为 18 秒)结束才能看到他们的分数.到目前为止,他们只能得到一分.我想要发生的是,一旦他们输入答案(无论正确与否),4 秒倒计时的内容就会重复.虽然,我希望在倒数计时器再次启动时也输出 loadG1.所以他们输入一个答案,一个新的随机 loadG1 再显示 4 秒然后消失,他们输入另一个答案,等等......直到更长的计时器结束.我该怎么做呢?我知道循环不起作用,因为我尝试过它只是使我的应用程序崩溃.如果有人能帮我做到这一点,并告诉我如何实现代码,我将不胜感激.注意:我是学习 Java 和 Android 开发者的新手.非常感谢.

In the game a random number (loadG1) outputted and is shown for 4 seconds. Once the four seconds are up, it disappears and the user must input its value to gain a point. Once the user presses enter, their input box disappears and the program must wait until the longer CountDownTimer (currently 18 seconds) has ended to see their score. So far, they can only score one point. What I want to happen is for the contents of the 4 second countdowntimer to be repeated once they enter an answer (whether correct or not). Although, I want loadG1 to also be outputted when they countdown timer starts again. So they enter an answer, a new random loadG1 is shown for another 4 secs then disappears, they enter another answer, etc... until the longer timer ends. How would I go about doing this? I'm aware that loops don't work, as I tried and it just crashed my app. I'd be very grateful if someone could help me do this, and show me how to implement the code. NOTE: I am new to learning Java and Android dev. Many thanks in advance.

代码如下:

public class game1 extends AppCompatActivity {

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


final Button loseStarter1;

    loseStarter1 = (Button) findViewById(R.id.Starter1);
    loseStarter1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            loseStarter1.setVisibility(View.GONE);

            final int[] score = {0};
            Random generateG1 = new Random();
            final int loadG1 = generateG1.nextInt(1000000)+10000;
            final TextView number = (TextView) findViewById(R.id.number);
            number.setText(" "+loadG1);

            new CountDownTimer(18000, 1000) {
                @Override
                public void onTick (long millisUntilFinished) {
                }
                public void onFinish() {
                    TextView result = (TextView) findViewById(R.id.outcome);
                    result.setText("Score: "+ score[0]);
                    TextView prompt = (TextView) findViewById(R.id.prompt);
                    prompt.setVisibility(View.GONE);
                }
            }.start();


                    new CountDownTimer(4000, 1000) {

                        @Override
                        public void onTick(long millisUntilFinished) {

                        }

                        @Override
                        public void onFinish() {
                            number.setVisibility(View.GONE);
                            final TextView prompt = (TextView) findViewById(R.id.prompt);
                            prompt.setText(" Enter the number");
                            final EditText input = (EditText) findViewById(R.id.enterAnswer);
                            input.setVisibility(View.VISIBLE);
                            input.setOnKeyListener(new View.OnKeyListener() {
                                @Override
                                public boolean onKey(View v, int keyCode, KeyEvent event) {
                                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                        switch (keyCode) {
                                            case KeyEvent.KEYCODE_ENTER:
                                                Editable answer = input.getText();
                                                int finalAnswer = Integer.parseInt(String.valueOf(answer));
                                                int finalLoadG1 = Integer.parseInt(String.valueOf(loadG1));
                                                input.setVisibility(View.GONE);
                                                prompt.setVisibility(View.GONE);
                                                if (finalAnswer == finalLoadG1) {
                                                    score[0]++;
                                                }

                                                return true;
                                            default:
                                        }
                                    }
                                    return false;
                                }
                            });
                        }
                    }.start();

            };
        });
    }

}

推荐答案

RESOLVED

我在 score[0]++ 后面加上了以下内容:

I put the following after the score[0]++:

number.setVisibility(View.VISIBLE);
final int loadG1 = generateG1.nextInt(1000000) + 10000;
number.setText(" " + loadG1);
input.getText().clear();

start();

这样,所有必要的更改都已完成,计时器重新开始.我还修改了 prompt TextView 的可见性,以确保它仅在用户能够输入答案时出现.我花了一段时间,但这已经解决了.

This way, all necessary changes are made and the timer begins anew. I also modified the visibility of the prompt TextView to ensure it only appear when the user is able to input an answer. Took me a while but this has been resolved.

这篇关于我怎样才能以特定的方式重复这个倒数计时器......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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