AndroidStudio项目制作倒计时模块

前言

大家好,给大家带来AndroidStudio项目制作倒计时模块的概述,希望你们喜欢

项目难度

AndroidStudio项目制作倒计时模块的难度,不是很大,就是主要用了TimerTimerTask这两个,接着就是现实界面的一些基础效果。

设计界面

做个倒计时的界面就比较好想了,就如下界面控件

  • 填写倒计时时间
  • 获取倒计时时间
  • 显示倒计时
  • 开始计时
  • 停止计时
    就在自动创建的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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="cn.edu.gdmec.android.counttime.MainActivity">
    <!--填写倒计时时间-->
    <EditText
        android:id="@+id/input"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"/>
    <!--获取倒计时时间-->
    <Button
        android:id="@+id/get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取倒计时时间"/>
    <!--显示倒计时-->
    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <!--开始计时-->
    <Button
        android:id="@+id/starttime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始计时"/>
    <!--停止计时-->
    <Button
        android:id="@+id/stoptime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止计时"/>
</LinearLayout>

实现功能需求

接下来我们需要在MainActivity.java中现实功能模块需求,主要来显示界面和获取按钮功能效果,代码如下:

package cn.edu.gdmec.android.counttime;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText inputet;
    private Button get, startTime, stopTime;
    private TextView time;
    private int i = 0;
    private Timer timer = null;
    private TimerTask task = null;

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

    private void initView() {
        inputet = findViewById(R.id.input);
        get = findViewById(R.id.get);
        startTime = findViewById(R.id.starttime);
        stopTime = findViewById(R.id.stoptime);
        time = findViewById(R.id.time);
        get.setOnClickListener(this);
        startTime.setOnClickListener(this);
        stopTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.get:
                time.setText(inputet.getText().toString());
                i = Integer.parseInt(inputet.getText().toString());
                break;
            case R.id.starttime:
                startTime();
                break;
            case R.id.stoptime:
                stopTime();
                break;
            default:
                break;
        }
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            time.setText(msg.arg1 + "");
            startTime();
        };
    };

    public void startTime() {
        timer = new Timer();
        task = new TimerTask() {

            @Override
            public void run() {
                if (i > 0) {   //加入判断不能小于0
                    i--;
                    Message message = mHandler.obtainMessage();
                    message.arg1 = i;
                    mHandler.sendMessage(message);
                }
            }
        };
        timer.schedule(task, 1000);
    }

    public void stopTime(){
        timer.cancel();
    }
}

心得重点

//获取的按钮实现:
time.setText(inputet.getText().toString());
i = Integer.parseInt(inputet.getText().toString());
//Handler的加入
private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            time.setText(msg.arg1 + "");
            startTime();
        };
    };
//倒计时主要核心
public void startTime() {
        timer = new Timer();
        task = new TimerTask() {

            @Override
            public void run() {
                if (i > 0) {   //加入判断不能小于0
                    i--;
                    Message message = mHandler.obtainMessage();
                    message.arg1 = i;
                    mHandler.sendMessage(message);
                }
            }
        };
        timer.schedule(task, 1000);
    }

总结

  • 本文讲了AndroidStudio项目制作倒计时模块,如果您还有更好地理解,欢迎沟通
  • 定位:分享 Android&Java知识点,有兴趣可以继续关注

原文地址:https://www.cnblogs.com/dashucoding/p/9276876.html

时间: 2024-08-30 07:06:04

AndroidStudio项目制作倒计时模块的相关文章

AndroidStudio项目制作SDK和打包成jar

这里将详细介绍AndroidStuido项目,开发过程中将AS项目打包成jar,及arr的使用 1.新建一个项目(步骤省略) 2.(在步骤1的基础上)点击File-->New-->New Module->选择Android Library-->点击Next(如下图:) 3.定义好Library的工程名:mylibrary(如下图: 4.在项目中会引入该Module (app下的build.gradle 5.创建之前我先在mylibrary工程中建一个测试类,方便测试调用: 6.在m

AndroidStudio项目提交(更新)到github最详细步骤

在使用studio开发的项目过程中有时候我们想将项目发布到github上,以前都是用一种比较麻烦的方式(cmd)进行提交,最近发现studio其实是自带这种功能的,终于可以摆脱命令行了. 因为自己也没有做很深的研究,这里就先分享一下通过studio将自己的项目上传到github上的步骤. 两个相关概念:git和github Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很小到非常大的项目版本管理.Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一

AndroidStudio项目提交(更新)到github最具体步骤

在使用studio开发的项目过程中有时候我们想将项目公布到github上.曾经都是用一种比較麻烦的方式(cmd)进行提交.近期发现studio事实上是自带这样的功能的,最终能够摆脱命令行了. 由于自己也没有做非常深的研究,这里就先分享一下通过studio将自己的项目上传到github上的步骤. 两个相关概念:git和github Git是一个开源的分布式版本号控制系统.用以有效.快速的处理从非常小到非常大的项目版本号管理.Git 是 Linus Torvalds 为了帮助管理 Linux 内核开

Androidstudio如何制作一个高仿小米计算器小demo

Androidstudio如何制作一个高仿小米计算器小demo ————安德风 一.最终成品效果图: 二.界面设计布局源代码:文件名activity_main.xml (存放在jsj(我的模块名为jsj)/res/layout/activity_main.xml) 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.a

Windows下AndroidStudio 中使用Git(AndroidStudio项目于GitHub关联)

前提条件 : 1. 安装 Git 客户端 下载链接 2. 有 GitHub 账号 (假设你已经有了一些git基础, 如果还一点都不会, 请去找其他加成学习) AndroidStudio项目发布到GitHub 1. 在AndroidStudio中新建一个项目(这个不能不知道吧!) 2. 设置: Settings -> Version Control -> Git , 设置git目录, 点击Test测试, 如果成功会有Success提示 3. 设置: Settring -> VersionC

Androidstudio项目分享到[email&#160;protected]托管

Androidstudio项目分享到[email protected]托管. 一.在OSC创建仓库 例如,创建一个AndroidStudy仓库,创建步骤如下: 输入仓库名称 点击创建按钮,就可以完成仓库的创建,如下图所示: 创建好之后,就可以拿到[email protected]仓库的HTTP连接 https://git.oschina.net/nycly/StudyGit.git 二.将项目托管到Git仓库 2.1.将项目变成一个Git项目 在AndroidStudio中创建一个Android

将Eclipse项目转换成AndroidStudio项目过程中遇到的问题以及解决方法

将Eclipse项目转换成AndroidStudio项目也不是第一次了,昨天转的时候遇到几个问题: 首先将项目导入androidstudio,导完后报错: 问题一: Error:java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: Error:Execution failed for task ':app:mergeDebugResources'.> Error: jav

androidstudio项目如何使用git版本回退

使用android studio 编写代码错误,有时可能会需要将项目版本回退到以前的某个版本上,这对于很多刚使用git的网友来说操作可能不是很懂,下面为大家整理了android studio 回退已经commit的代码和有使用git提交代码仓库中的程序回退方法. androidstudio项目如何使用git版本回退 对于commit(不是push)的代码如何回滚,这块对于刚转到git的开发者还是比较陌生的.今天我来说下这块的内容: 按照惯例,先上图,下图是还没有commit任何代码的push界面

Ecplise项目和AndroidStudio项目之间的转换

Ecplise项目和AndroidStudio项目之间的转换 为了帮助一些刚接触AndroidStudio的童鞋,在这里我把自己琢磨出来的一点经验分享给大家!Ecplise项目变为AS项目有两种方式,一种只不改变原有的项目结构,只是用AS进行编译Ecplise项目(适合Ecplise和AS共存的的开发),另一种是改变目录结构,和新建AS项目一样使用Gradle进行编译. 直接使用AndroidStudio打开Ecplise项目 在Ecplise项目结构如下图 找到工程的文件路径 接下来打开AS