Android 中不用线程如何实现倒计时

所属分类: 软件编程 / Android 阅读数: 27
收藏 0 赞 0 分享

需求:

有多个组件可以开启倒计时,正常情况下默认倒计时时间终了后更新UI,另,用户可以取消指定倒计时。

这里使用CountDownTimer进行倒计时,其中回调函数onFinish是在倒计时终了时回调,onTick是在倒计时开始时回调,用户可以使用CountDownTimer对象的cancel方法取消倒计时。

这样做的好处:不需要使用繁琐的线程去控制倒计时,更方便的进行UI更新。

上代码:

MainActivity

package test.demo.countdowntest;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  private Button bt1, bt2, bt3;
  private ProgressBar pb1, pb2, pb3;
  private MyCount mc1,mc2, mc3;
  private boolean mc1Click = false;
  private boolean mc2Click = false;
  private boolean mc3Click = false;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    bt1 = ((Button) findViewById(R.id.bt1));
    bt2 = ((Button) findViewById(R.id.bt2));
    bt3 = ((Button) findViewById(R.id.bt3));
    bt1.setOnClickListener(this);
    bt2.setOnClickListener(this);
    bt3.setOnClickListener(this);
    pb1 = ((ProgressBar) findViewById(R.id.pb1));
    pb2 = ((ProgressBar) findViewById(R.id.pb2));
    pb3 = ((ProgressBar) findViewById(R.id.pb3));
    mc1 = new MyCount(30000, 1000);
    mc1.setPb(bt1, pb1);
    mc2 = new MyCount(30000, 1000);
    mc2.setPb(bt2, pb2);
    mc3 = new MyCount(30000, 1000);
    mc3.setPb(bt3, pb3);
  }
  @Override
  public void onClick(View view) {
    switch (view.getId()) {
      case R.id.bt1:
        if (mc1Click) {
          mc1.cancel();
          pb1.setVisibility(View.GONE);
          mc1Click = false;
        } else {
          pb1.setVisibility(View.VISIBLE);
          mc1.start();
          mc1Click = true;
        }
        break;
      case R.id.bt2:
        if (mc2Click) {
          pb2.setVisibility(View.GONE);
          mc2.cancel();
          mc2Click = false;
        } else {
          pb2.setVisibility(View.VISIBLE);
          mc2.start();
          mc2Click = true;
        }
        break;
      case R.id.bt3:
        if (mc3Click) {
          pb3.setVisibility(View.GONE);
          mc3.cancel();
          mc3Click = false;
        } else {
          pb3.setVisibility(View.VISIBLE);
          mc3.start();
          mc3Click = true;
        }
        break;
    }
  }
  /*定义一个倒计时的内部类*/
  class MyCount extends CountDownTimer {
    Button mBt;
    ProgressBar mPb;
    public MyCount(long millisInFuture, long countDownInterval) {
      super(millisInFuture, countDownInterval);
    }
    public void setPb(Button bt, ProgressBar pb) {
      mBt = bt;
      mPb = pb;
    }
    @Override
    public void onFinish() {
      mPb.setVisibility(View.GONE);
    }
    @Override
    public void onTick(long millisUntilFinished) {
      mBt.setText("请等待30秒(" + millisUntilFinished / 1000 + ")...");
      Toast.makeText(MainActivity.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();
    }
  }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:orientation="vertical"
  tools:context="cn.sh.changxing.countdowntest.MainActivity">
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
      android:id="@+id/bt1"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="测试启动1"/>
    <ProgressBar
      android:id="@+id/pb1"
      style="?android:attr/progressBarStyleSmall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:indeterminate="true"/>
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
      android:id="@+id/bt2"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="测试启动2"/>
    <ProgressBar
      android:id="@+id/pb2"
      style="?android:attr/progressBarStyleSmall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:indeterminate="true"/>
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
      android:id="@+id/bt3"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:text="测试启动3"/>
    <ProgressBar
      android:id="@+id/pb3"
      style="?android:attr/progressBarStyleSmall"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:indeterminate="true"/>
  </LinearLayout>
</LinearLayout>

以上所述是小编给大家介绍的Android 中不用线程如何实现倒计时,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

更多精彩内容其他人还在看

android byte[] 和short[]转换的方法代码

这篇文章主要介绍了android byte[] 和short[]转换的方法代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取应用程序大小的方法

这篇文章主要介绍了Android获取应用程序大小的方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android获取其他包的Context实例代码

这篇文章主要介绍了Android获取其他包的Context实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android放大镜的实现代码

这篇文章主要介绍了Android放大镜的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android 读取Properties配置文件的小例子

这篇文章主要介绍了Android 读取Properties配置文件的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Android通讯录开发之删除功能的实现方法

这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
收藏 0 赞 0 分享

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享
查看更多