countdown timer plus_android studio计时器

countdown timer plus_android studio计时器Inthisandroidcountdowntimerexample,we’llimplementatimerobjecttodisplaytheprogressinaProgressBar.Theapplicationwe’llbuildinthistutorialisausefulcomponentinQuizappswhere…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

In this android countdown timer example, we’ll implement a timer object to display the progress in a ProgressBar. The application we’ll build in this tutorial is a useful component in Quiz apps where the time left to complete the level is displayed graphically to enhance the user experience.

在此android countdown计时器示例中,我们将实现一个timer对象以在ProgressBar中显示进度 。 我们将在本教程中构建的应用程序是测验应用程序中的有用组件,其中,完成关卡所需的时间以图形方式显示,以增强用户体验。

Android CountDownTimer (Android CountDownTimer)

Android CountDownTimer class is used to schedule a countdown until a time in the future defined by the user, with regular notifications on intervals along the way. This class is an abstract class whose methods need to be overridden to implement it in our project. The following line needs to be added in our activity to import the class :

Android CountDownTimer类用于安排倒计时,直到用户定义未来的某个时间为止,并定期通知间隔时间。 此类是一个抽象类,其方法必须重写才能在我们的项目中实现。 在我们的活动中需要添加以下行以导入该类:

import android.os.CountDownTimer;

import android.os.CountDownTimer;

The relevant methods of the CountDownTimer Class are given below.

CountDownTimer类的相关方法如下。

  1. synchronized final void cancel() : This is used to cancel the countdown

    synchronized final void cancel() :用于取消倒计时

  2. abstract void onFinish() : This callback method is fired when the timer finishes

    abstract void onFinish() :计时器结束时会触发此回调方法

  3. abstract void onTick(long millisUntilFinished) : This callback method is fired on regular intervals

    abstract void onTick(long millisUntilFinished) :定期触发此回调方法

  4. synchronized final CountDownTimer start() : This method is used to start the countdown

    synchronized final CountDownTimer start() :此方法用于启动倒计时

The signature of the public constructor of the CountDownTimer class is given below.

CountDownTimer类的公共构造函数的签名如下所示。

CountDownTimer(long millisInFuture, long countDownInterval)

CountDownTimer(long millisInFuture, long countDownInterval)

The parameters of the constructors are defined as follows :

构造函数的参数定义如下:

  • millisInFuture : The number of milli seconds in the future from the call to start() until the countdown is done and onFinish() is called

    millisInFuture :从调用start()到倒计时完成并调用onFinish()以后的毫秒数。

  • countDownInterval : The interval along the way to receive onTick(long) callbacks

    countDownInterval :接收onTick(long)回调的时间间隔

In this project we’ll update the time values in a ProgressBar as the onTick() method is invoked repeatedly.

在此项目中,由于onTick()方法被重复调用,我们将更新ProgressBar中的时间值。

Android倒数计时器示例项目结构 (Android Countdown Timer Example Project Structure)

Android倒数计时器代码 (Android Countdown Timer Code)

The activity_main.xml consists of two buttons i.e. the start and stop timer buttons and a ProgressBar to display the time.

activity_main.xml由两个按钮组成,即开始和停止计时器按钮以及用于显示时间的ProgressBar。

activity_main.xml

activity_main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:max="10"
        android:minHeight="50dp"
        android:minWidth="200dp"
        android:progress="0"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Timer"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="61dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop Timer"
        android:id="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="46dp"
        android:layout_below="@+id/progressBar" />

</RelativeLayout>

The MainActivity.java is given below :

MainActivity.java如下所示:

package com.journaldev.countdowntimer;

import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    ProgressBar progressBar;
    Button start_timer,stop_timer;
    MyCountDownTimer myCountDownTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressBar=(ProgressBar)findViewById(R.id.progressBar);
        start_timer=(Button)findViewById(R.id.button);
        stop_timer=(Button)findViewById(R.id.button2);

        start_timer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                myCountDownTimer = new MyCountDownTimer(10000, 1000);
                myCountDownTimer.start();

            }
        });

        stop_timer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                myCountDownTimer.cancel();

            }
        });

    }

    public class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {

            int progress = (int) (millisUntilFinished/1000);

            progressBar.setProgress(progressBar.getMax()-progress);
        }

        @Override
        public void onFinish() {
            finish();
        }
    }
}

In the above code we’ve defined an Anonymous Inner Class called MyCountDownTimer. In this example we’ve set a Timer for 10 seconds that updates after every second. By default the timer displays/updates the time in decreasing order ( as its named CountDown!), Hence to show the progress in increasing order we’ve subtracted the time from the max time.

在上面的代码中,我们定义了一个名为MyCountDownTimer匿名内部类 。 在此示例中,我们将计时器设置为10秒,该计时器每秒钟更新一次。 默认情况下,计时器以降序显示/更新时间(称为CountDown!),因此,为了以升序显示进度,我们从最大时间中减去了时间。

The timer once stopped restarts from the beginning. Below is our android countdown timer app in action.

一旦停止计时器将重新开始。 以下是我们运行中的android倒数计时器应用程序。

This brings an end to countdown timer android tutorial. You can download the final Android CountDownTimer Project from the below link.

这样就结束了倒数计时器Android教程。 您可以从下面的链接下载最终的Android CountDownTimer项目

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/9896/android-countdowntimer-example

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193419.html原文链接:https://javaforall.net

(0)
上一篇 2025年11月25日 下午9:01
下一篇 2025年11月25日 下午9:43


相关推荐

  • 小程序 生命周期

    小程序 生命周期生命周期指的就是小程序从创建到销毁的过程小程序生命周期分为应用生命周期和页面生命周期应用生命周期影响页面生命周期应用生命周期应用生命周期是在 app js 里面调用的属性说明 onLaunch 应用第一次启动的就会触发的事件 onShow 应用被用户看到 onHide 用户被隐藏 onError 应用的代码发生了报错的时候就会触发 onPageNotFou 页面找不到就会触发代码示例 onLaunch 第一次启动的时候就会触发 onLaunchApp

    2026年3月26日
    3
  • windows-2016搭建AD域服务器教程

    windows-2016搭建AD域服务器教程1 AD 域 AD 是 ActiveDirect 的缩写 即活动目录 DomainContro 是一台计算机 实现用户 计算机 目录的统一管理 AD 活动目录 是一种存储协议 基于 LDAP 2 域控制器 DCDC 是 DomainContro 的缩写 即域控制器 域控制器是通过活动目录 AD 提供服务 一个域内可以有多台的域控制器 而在大部分情况下 每一台域控制器的地位是平等的 它们各存储着一份相同的活动目录 1 在服务器管理器里面添加角色与功能 2 勾选 AD 域服务功能 3 安装完成后

    2026年3月17日
    2
  • ARM方案公司,三星S5PV210核心板,「建议收藏」

    ARM方案公司,三星S5PV210核心板,「建议收藏」核心业务:高通、MTK、三星等解决方案定制开发

    2022年10月14日
    4
  • redis分布式锁的应用场景有哪些_redis setnx 分布式锁

    redis分布式锁的应用场景有哪些_redis setnx 分布式锁“分布式锁”是用来解决分布式应用中“并发冲突”的一种常用手段,实现方式一般有基于zookeeper及基于redis二种。具体到业务场景中,我们要考虑二种情况:一、抢不到锁的请求,允许丢弃(即:忽略)比如:一些不是很重要的场景,比如“监控数据持续上报”,某一篇文章的“已读/未读”标识位更新,对于同一个id,如果并发的请求同时到达,只要有一个请求处理成功,就算成功。用活动图表示如下:二、并发请求,不论哪一条都必须要处理的场景(即:不允许丢数据)比如:一个订单,客户正在前台修改地址,管理员在后台同时修

    2025年10月8日
    5
  • 设备树 dtb结构

    设备树 dtb结构dtb 结构由一个小的报头和三个大小可变的部分组成 内存预留块 结构块和字符串块 这些应该按这个顺序出现在扁平的设备树中 因此 设备树结构作为一个整体 当以地址载入内存时 将类似于下图的图 较低的地址位于图的顶部 注 内存预留块可能不存在 尽管在某些情况下可能需要它们来满足单个块的对齐约束 自格式的原始定义以来 已经定义了几种扁平设备树结构的版本 报头中的字段给出了版本 以便客户端程序可以确定设备树是否以兼容的格式编码 本文档仅描述 17 版的格式 兼容 DTSpec 的引导程

    2025年6月22日
    4
  • dsp运动控制卡_营销行动方案控制

    dsp运动控制卡_营销行动方案控制ARM+FPGA运动控制卡运动控制卡方案运动控制卡方案运动控制卡方案由于ARM源码核心运动控制算法部分缺失,因此便宜出售此资料,拍前请了解好,不接受退货,资料包含此运动控制卡原理图,PCB图,FPGA源码,ARM去掉算法后的框架源码,拍下后发邮箱。本运动控制卡采用ARM单片机+FPGA架构;ARM单片机是基于Cortex-M3内核的LM3S6911,插补核心算法均在该ARM内完成,一方面通过以太网与上位机界面交换加工数据,另一方面与FPGA(ALTERA的EP1C3)交换加工脉冲计数与IO

    2022年10月15日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号