如何延长倒计时有了暂停? [英] How to extend CountDown Timer With Pause?

查看:162
本文介绍了如何延长倒计时有了暂停?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个常见的​​问题,我怎么暂停countdowntimer?

This seems to be a common question, "how do I pause a countdowntimer"?

我发现这个code这看起来很有希望:

I found this code which looks promising:

<一个href=\"http://www.java2s.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm\" rel=\"nofollow\">http://www.java2s.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm

我只是不知道如何实现它在code。我需要重写的抽象方法和扩展类。有人可以给我一个想法,如何做到这一点?

I'm just not sure how to implement it in code. I need to override the abstracted methods and extend the class. Can someone give me an idea how to do this?

推荐答案

下面是你的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="0"
            android:gravity="center_horizontal"
            android:layout_marginBottom="40dp"
            android:layout_marginTop="20dp"
            android:textSize="27dp"
            android:id="@+id/txtView"/>
    <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="center">
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start"
                android:id="@+id/startButton"/>
        <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Stop"
                android:id="@+id/stopButton"
                android:layout_toRightOf="@id/startButton"/>
    </RelativeLayout>
</LinearLayout>



这里是你的活动类


Here is your activity class

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyActivity extends Activity implements View.OnClickListener {

    TextView txt = null;
    Button startButton = null;
    Button stopButton = null;
    CountDownTimerWithPause timer = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        txt = (TextView) findViewById(R.id.txtView);
        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);

        startButton.setOnClickListener(this);
        stopButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == startButton) {
            if (timer == null) {
                initTimer();
            }

            timer.resume();
        }

        if (view == stopButton) {
            timer.pause();
        }
    }

    private void initTimer() {
        timer = new CountDownTimerWithPause(10000, 1000, false) {
            @Override
            public void onTick(long millisUntilFinished) {
                Integer currentValue = Integer.valueOf((String) MyActivity.this.txt.getText());
                MyActivity.this.txt.setText(String.valueOf(currentValue + 1));
            }

            @Override
            public void onFinish() {
                Toast.makeText(MyActivity.this, "finish", Toast.LENGTH_SHORT).show();
            }
        };

        timer.create();
    }
}

不要忘了复制<一个href=\"http://www.java2s.com/Open-Source/Android/Timer/multitimer-android/com/cycleindex/multitimer/CountDownTimerWithPause.java.htm\"相对=nofollow> CountDownTimerWithPause 到你的包。

对我的作品, BUT ...

Works for me, BUT...

我提了一些错误,但它取决于你的应用程序。也许这是你确定。错误是 - 启动计时器时,它触发immediatelly。 '所以呢?'你可能会问。想象一下,你点击停止按钮时3个半秒钟过去了。所以,当您单击开始按钮,你希望你看到4前半秒传球,但不是在类实现。

I mention some bugs but it depends on your application. Maybe it's OK for you. Bug is - when starting timer, it fires immediatelly. 'So what?' you might ask. Imagine you click Stop button when 3 and half seconds passed. So when you click Start button you expect half second pass before you see 4, but not in that class implementation.

和有一个在计时结束一些奇怪的延迟。我建议,以寻求更好的落实。

And there is some strange delay at the end of counting. I'd recommend to search for better implementation.

这篇关于如何延长倒计时有了暂停?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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