AndroidStudio项目制作倒计时模块
前言
大家好,给大家带来AndroidStudio项目制作倒计时模块的概述,希望你们喜欢
项目难度
AndroidStudio项目制作倒计时模块的难度,不是很大,就是主要用了Timer和TimerTask这两个,接着就是现实界面的一些基础效果。
设计界面
做个倒计时的界面就比较好想了,就如下界面控件
- 填写倒计时时间
- 获取倒计时时间
- 显示倒计时
- 开始计时
- 停止计时
就在自动创建的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项目制作倒计时模块的更多相关文章
- JavaWeb-SpringBoot(抖音)_一、抖音项目制作
JavaWeb-SpringBoot(抖音)_一.抖音项目制作 传送门 JavaWeb-SpringBoot(抖音)_二.服务器间通讯 传送门 JavaWeb-SpringBoot(抖音)_三.抖音项 ...
- AndroidStudio项目提交(更新)到github最详细步骤
在使用studio开发的项目过程中有时候我们想将项目发布到github上,以前都是用一种比较麻烦的方式(cmd)进行提交,最近发现studio其实是自带这种功能的,终于可以摆脱命令行了. 因为自己也没 ...
- AndroidStudio项目提交(更新)到github最具体步骤
在使用studio开发的项目过程中有时候我们想将项目公布到github上.曾经都是用一种比較麻烦的方式(cmd)进行提交.近期发现studio事实上是自带这样的功能的,最终能够摆脱命令行了. 由于自己 ...
- AndroidStudio项目提交到github最详细步骤
在使用studio开发的项目过程中有时候我们想将项目发布到github上,以前都是用一种比较麻烦的方式(cmd)进行提交,最近发现studio其实是自带这种功能的,终于可以摆脱命令行了. 因为自己也没 ...
- AndroidStudio项目提交到github最详细步骤【转】
感谢大佬:https://www.cnblogs.com/imqsl/p/6763133.html 在使用studio开发的项目过程中有时候我们想将项目发布到github上,以前都是用一种比较麻烦的方 ...
- 基于dubbo构建分布式项目与服务模块
关于分布式服务架构的背景和需求可查阅http://dubbo.io/.不同于传统的单工程项目,本文主要学习如何通过maven和dubbo将构建分布项目以及服务模块,下面直接开始. 创建项目以及模块 ...
- Go项目结构和模块导入
Go项目结构和模块导入 golang项目结构与其他语言类似,但是仍然有一些需要注意的地方. 项目结构 环境配置 go 命令依赖一个重要的环境变量:$GOPATH,它表示GO项目的路径,如下设置 exp ...
- Windows下AndroidStudio 中使用Git(AndroidStudio项目于GitHub关联)
前提条件 : 1. 安装 Git 客户端 下载链接 2. 有 GitHub 账号 (假设你已经有了一些git基础, 如果还一点都不会, 请去找其他加成学习) AndroidStudio项目发布到Git ...
- Androidstudio项目分享到Git@OSC托管
Androidstudio项目分享到Git@OSC托管. 一.在OSC创建仓库 例如,创建一个AndroidStudy仓库,创建步骤如下: 输入仓库名称 点击创建按钮,就可以完成仓库的创建,如下图所示 ...
随机推荐
- python入门 -- 学习笔记2
习题11:提问 -- 接受键盘的输入 raw_input input() 和 raw_input() 有何不同? input() 函数会把你输入的东西当做 Python 代码进行处理,这么做会有安 ...
- ubuntu系统中安装eclipse
具体可以看这篇博文 .https://www.cnblogs.com/sanduo1314/articles/5137090.html 然后再/usr/share/applications中找到ecl ...
- yii2.0如何优化路由
比如我的路由是 http://localhost/basic/web/?r=site/index 现在想改成 http://localhost/basic/web/site/index 的形式 ...
- 基于python的selenium自动化测试环境搭建
Windows下的环境搭建: 1.安装python2.7.152.cmd里敲pip install selenium3.安装firefox47.geckodriver11(并将geckodriver. ...
- react项目搭建
1.下载安装node.js,需要node.js环境. 2.经过挑选,决定选择creat-react-app这个项目脚手架,然后输入指令安装 $ npm install -g crea ...
- [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 ...
- [leetcode]18. 4Sum四数之和
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- Spring MVC中@JsonView的使用
一.@JsonView注解的简介 @JsonView是jackson json中的一个注解,Spring webmvc也支持这个注解,它的作用就是控制输入输出后的json 二.@JsonView注解的 ...
- rpo攻击
0 什么是RPO攻击? RPO(Relative Path Overwrite)相对路径覆盖,是一种新型攻击技术,最早由Gareth Heyes在其发表的文章中提出.主要是利用浏览器的一些特性和部分服 ...
- php正则提取html图片(img)src地址与任意属性的方法
<?php /*PHP正则提取图片img标记中的任意属性*/ $str = '<center><img src="/uploads/images/2017020716 ...