android项目倒计时,AndroidStudio项目制作倒计时模块

前言

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

项目难度

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

设计界面

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

填写倒计时时间

获取倒计时时间

显示倒计时

开始计时

停止计时 就在自动创建的activity_main.xml中写入代码:

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">

android:id="@+id/input"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:ems="10"/>

android:id="@+id/get"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="获取倒计时时间"/>

android:id="@+id/time"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

/>

android:id="@+id/starttime"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="开始计时"/>

android:id="@+id/stoptime"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="停止计时"/>

实现功能需求

接下来我们需要在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知识点,有兴趣可以继续关注

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值