前言

大家好,给大家带来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知识点,有兴趣可以继续关注

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

  1. JavaWeb-SpringBoot(抖音)_一、抖音项目制作

    JavaWeb-SpringBoot(抖音)_一.抖音项目制作 传送门 JavaWeb-SpringBoot(抖音)_二.服务器间通讯 传送门 JavaWeb-SpringBoot(抖音)_三.抖音项 ...

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

    在使用studio开发的项目过程中有时候我们想将项目发布到github上,以前都是用一种比较麻烦的方式(cmd)进行提交,最近发现studio其实是自带这种功能的,终于可以摆脱命令行了. 因为自己也没 ...

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

    在使用studio开发的项目过程中有时候我们想将项目公布到github上.曾经都是用一种比較麻烦的方式(cmd)进行提交.近期发现studio事实上是自带这样的功能的,最终能够摆脱命令行了. 由于自己 ...

  4. AndroidStudio项目提交到github最详细步骤

    在使用studio开发的项目过程中有时候我们想将项目发布到github上,以前都是用一种比较麻烦的方式(cmd)进行提交,最近发现studio其实是自带这种功能的,终于可以摆脱命令行了. 因为自己也没 ...

  5. AndroidStudio项目提交到github最详细步骤【转】

    感谢大佬:https://www.cnblogs.com/imqsl/p/6763133.html 在使用studio开发的项目过程中有时候我们想将项目发布到github上,以前都是用一种比较麻烦的方 ...

  6. 基于dubbo构建分布式项目与服务模块

      关于分布式服务架构的背景和需求可查阅http://dubbo.io/.不同于传统的单工程项目,本文主要学习如何通过maven和dubbo将构建分布项目以及服务模块,下面直接开始. 创建项目以及模块 ...

  7. Go项目结构和模块导入

    Go项目结构和模块导入 golang项目结构与其他语言类似,但是仍然有一些需要注意的地方. 项目结构 环境配置 go 命令依赖一个重要的环境变量:$GOPATH,它表示GO项目的路径,如下设置 exp ...

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

    前提条件 : 1. 安装 Git 客户端 下载链接 2. 有 GitHub 账号 (假设你已经有了一些git基础, 如果还一点都不会, 请去找其他加成学习) AndroidStudio项目发布到Git ...

  9. Androidstudio项目分享到Git@OSC托管

    Androidstudio项目分享到Git@OSC托管. 一.在OSC创建仓库 例如,创建一个AndroidStudy仓库,创建步骤如下: 输入仓库名称 点击创建按钮,就可以完成仓库的创建,如下图所示 ...

随机推荐

  1. spring-security使用

    极客学院Spring Security 例子 <?xml version="1.0" encoding="UTF-8"?> <beans:be ...

  2. PHP和Redis实现在高并发下的抢购及秒杀功能示例详解

    抢购.秒杀是平常很常见的场景,面试的时候面试官也经常会问到,比如问你淘宝中的抢购秒杀是怎么实现的等等. 抢购.秒杀实现很简单,但是有些问题需要解决,主要针对两个问题: 一.高并发对数据库产生的压力二. ...

  3. 2D射影空间,为何引入射影空间

    2D欧氏空间R2中,点的表示是A(x1,y1), B(x2,y2),二维参数,线的表示是L: y=kx+b,是二维参数: 如何表示点在线上面?可以扩展为(k,-1,b)* (x1,y1,1)t = 0 ...

  4. Array.prototype.push.apply(a,b)和Array.prototype.slice.call(arguments)

    Array.prototype.push.apply(a,b) 时常看到在操作数组的时候有这样的写法: var a = [1,2,3]; var b = [4,5,6]; a.push.apply(a ...

  5. 通过windows远程桌面连接CentOS系统

    前提: CentOS安装桌面,如果无桌面,请执行 # yum -y groups install "GNOME Desktop" # startx 1 2 配置源 # yum in ...

  6. Java学习笔记(二十):多态

    什么是多态 多态的好处 举个例子:需求:给饲养员提供一个喂养动物的方法,用于喂养动物 假如没有多态,会发现针对不同类型的动物,我们需要提供不同的feed方法来喂养,当需求变化时,比如增加动物,就要增加 ...

  7. Discuz!开发之HTML转Discuz代码(bbcode)函数html2bbcode()

    定义文件:\source\function\function_editor.php函数定义: function html2bbcode($text) { $text = strip_tags($tex ...

  8. protobuf shutdownprotobuflibrary的时候crash,释放的指针出错

    往往是多个子项目中有多次链接使用. 解决方法: 1. 使用静态库. 2. issure中有说2.6.1还未允许多次释放,建议使用3.4.x版本. 参考: https://github.com/prot ...

  9. [leetcode]54. Spiral Matrix螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  10. [leetcode]35. Search Insert Position寻找插入位置

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...