How can I repeat this countdown timer in a specific way…?

空扰寡人 提交于 2019-11-28 12:51:48

问题


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.

Here's the code:

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();

            };
        });
    }

}

回答1:


RESOLVED

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();

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.



来源:https://stackoverflow.com/questions/40057754/how-can-i-repeat-this-countdown-timer-in-a-specific-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!