Android 如何延长带暂停的倒计时计时器?

Android 如何延长带暂停的倒计时计时器?,android,timer,countdown,Android,Timer,Countdown,这似乎是一个常见的问题,“如何暂停倒计时” 我发现这段代码看起来很有希望: 我只是不知道如何在代码中实现它。我需要重写抽象方法并扩展类。有人能告诉我怎么做吗?这是您的布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertica

这似乎是一个常见的问题,“如何暂停倒计时”

我发现这段代码看起来很有希望:

我只是不知道如何在代码中实现它。我需要重写抽象方法并扩展类。有人能告诉我怎么做吗?

这是您的布局

<?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>
别忘了把它复制到你的包裹里

对我有用,但是

我提到了一些错误,但这取决于您的应用程序。也许对你来说没问题。错误是——当启动定时器时,它会立即触发“那又怎样?”你可能会问。想象一下,当3秒半过去时,您单击停止按钮。所以,当您单击Start按钮时,您希望在看到4之前经过半秒,但在该类实现中不是这样


计数结束时有一些奇怪的延迟。我建议搜索更好的实现。

我在我的项目中使用了
CountDownTimerWithPause
,它非常简单。 看看我刚刚编写的示例代码,它解释了这个计数器的实现

第一步 我创建了示例XML布局,只是为了以后更好地解释。此布局包含3个按钮:

  • 开始
  • 停顿
  • 恢复
  • 布局:

    <Button
        android:id="@+id/button_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" />
    
    <Button
        android:id="@+id/button_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause" />
    
    <Button
        android:id="@+id/button_resume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Resume" />
    
    很容易看出我是如何用pause扩展了countdowntimerwhithpause的,以及应该调用哪些方法来启动、暂停和恢复计数器


    希望我能解释一下这个柜台的用法。

    怎么办?如何扩展类?如何使用类。我不知道该怎么办。@KristyWelsh:因为u提供的链接不是countdowntimer的示例,这是countdowntimer类的源代码是的,我是android新手。我不确定我将如何实际使用此代码来替代CountDownTimer类。倒计时没有暂停或恢复方法。请稍等,我将为您编写演示项目。谢谢!我同意,有一些奇怪的行为正在发生。我可能得去别处找。
    <Button
        android:id="@+id/button_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" />
    
    <Button
        android:id="@+id/button_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause" />
    
    <Button
        android:id="@+id/button_resume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Resume" />
    
    public class MainActivity extends Activity
    {
    
        private CountDownTimerWithPause _countDownTimerWithPause = null;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.timer);
    
            /*
             * Definition of the counter
             */
            _countDownTimerWithPause = new CountDownTimerWithPause(10000, 1000, true)
            {
    
                @Override
                public void onTick(long millisUntilFinished)
                {
                    Toast.makeText(getApplicationContext(), "Tick", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onFinish()
                {
                    Toast.makeText(getApplicationContext(), "Finished", Toast.LENGTH_SHORT).show();
                }
            };
    
            /*
             * Click to start the counter
             */
            Button buttonStart = (Button) findViewById(R.id.button_start);
            buttonStart.setOnClickListener(new View.OnClickListener()
            {
    
                @Override
                public void onClick(View v)
                {
                    _countDownTimerWithPause.create();
                }
            });
    
            /*
             * Click to pause the counter
             */
            Button buttonPause = (Button) findViewById(R.id.button_pause);
            buttonPause.setOnClickListener(new View.OnClickListener()
            {
    
                @Override
                public void onClick(View v)
                {
                    _countDownTimerWithPause.pause();
                }
            });
    
            /*
             * Click to resume the counter
             */
            Button buttonResume = (Button) findViewById(R.id.button_resume);
            buttonResume.setOnClickListener(new View.OnClickListener()
            {
    
                @Override
                public void onClick(View v)
                {
                    _countDownTimerWithPause.resume();
                }
            });
        }
    
    }