3

CountDownTimer的 StartButton 有问题:按下按钮后计时器没有启动。我该如何解决?

我想通过按下按钮来启动计时器buttonCount。有人能帮助我吗?

int clicks = 0;
TextView textCount;
ImageButton buttonCount;
int guessCount =0;
boolean started = false;
boolean timerProcessing = false;
private CountDownTimer count;



protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newgame);

        count = new CountDownTimer(15000, 1000) {
            public void onTick(long millisUntilFinished) {
                int seconds = (int) ((millisUntilFinished / 1000));
                textic.setText("Time Left: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                textic.setText("Time's Up!");
                buttonCount.setEnabled(false);
                if (clicks > oldscore)
                    getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
            }
        };

        final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);
        final TextView textView = (TextView) findViewById(R.id.applesEaten);

        buttonCount = (ImageButton) findViewById(R.id.button);
        buttonCount.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                clicks++;
                textView.setText("Clicks: " + clicks);
                TextView textView = (TextView) findViewById(R.id.topScoreView);
                textView.setText("Best: " + oldscore);

                if(!started){
                    count.start();
                    started = true;
                    timerProcessing = true;
                }
            }
        });
        final TextView textic = (TextView) findViewById(R.id.textView2);
    }
4

1 回答 1

1

在我看来,这是您真正想要做的:

    private int clicks = 0;
    private TextView textCount;
    private ImageButton buttonCount;
    private int guessCount = 0;  
    private CountDownTimer count; // RENAMED     
    private boolean started = false; // FALSE. 
    private boolean timerProcessing = false;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newgame);

        count = new CountDownTimer(15000, 1000) { // MOVED UP
            public void onTick(long millisUntilFinished) {
                int seconds = (int) ((millisUntilFinished / 1000));
                textic.setText("Time Left: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                textic.setText("Time's Up!");
                buttonCount.setEnabled(false);
                if (clicks > oldscore)
                    getSharedPreferences("myPrefs", MODE_PRIVATE).edit().putInt("highscore", clicks).commit();
            }
        };


        final int oldscore = getSharedPreferences("myPrefs", MODE_PRIVATE).getInt("highscore", 0);
        final TextView textView = (TextView) findViewById(R.id.applesEaten);

        buttonCount = (ImageButton) findViewById(R.id.button);
        buttonCount.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                clicks++;
                textView.setText("Clicks: " + clicks);
                TextView textView = (TextView) findViewById(R.id.topScoreView);
                textView.setText("Best: " + oldscore);

                if(!started){
                    count.start(); // START COUNTDOWN TIMER
                    started = true;
                    timerProcessing = true;

                }

            }
        });
        final TextView textic = (TextView) findViewById(R.id.textView2);


    }

如果你真的想启动另一个 CountDownTimer,而不是你在底部创建的那个(命名为 count)。然后你需要实例化它并设置它的行为,就像你对另一个 CountownTimer 所做的那样。

此外,您使用的所有变量都需要在之前创建(textic,oldscore)

于 2014-08-19T21:43:50.123 回答