作者:程序员小冰,GitHub主页:https://github.com/QQ986945193
新浪微博:http://weibo.com/mcxiaobing
首先给大家看一下我们今天这个最终实现的效果图:
界面效果有点丑,不过功能齐全。大家如果需要,可以集成到自己的项目中。
首先说明,其实这个很简单的,利用了一个定时器而已。不过我这里进行了
输入的时间进行判断,比如不是Int类型均不可以进行倒计时。还有防止
多次点击开始计时,最后就是进行了回收对象。好了给大家看一下代码吧。
xml布局比较简单,几个button与textview,edittext实现的。
<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">
<EditText
android:id="@+id/et_timer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入计时时间" />
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="获取计时时间" />
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="计时时间" />
<Button
android:id="@+id/btn_go"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="开始计时" />
<Button
android:id="@+id/btn_stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止计时" />
</LinearLayout>
下面是java类中的代码:
package davidtimerdown.qq986945193.com.davidtimerdown;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author :程序员小冰
* @新浪微博 :http://weibo.com/mcxiaobing
* @GitHub: https://github.com/QQ986945193
* @CSDN博客: http://blog.csdn.net/qq_21376985
* @码云OsChina :http://git.oschina.net/MCXIAOBING
*/
public class MainActivity extends Activity implements OnClickListener {
protected static final int MSG_WHAT = 0;
private Button btn_go;
private Button btn_get;
private Button btn_stop;
private static EditText et_time;
private static TextView tv_time;
private Timer timer;
private static int time;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
tv_time.setText(time + "");
switch (msg.what) {
case MSG_WHAT:
if (time > 0) {
time--;
} else {
Toast.makeText(MainActivity.this, "倒计时完成", Toast.LENGTH_SHORT).show();
if (timer != null) {
timer.cancel();
timer = null;
}
}
break;
default:
break;
}
}
;
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_go = (Button) findViewById(R.id.btn_go);
btn_get = (Button) findViewById(R.id.btn_get);
btn_stop = (Button) findViewById(R.id.btn_stop);
et_time = (EditText) findViewById(R.id.et_timer);
tv_time = (TextView) findViewById(R.id.tv_time);
btn_go.setOnClickListener(this);
btn_get.setOnClickListener(this);
btn_stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_get:
if (et_time.getText().toString().trim() != null && !(et_time.getText().toString().trim().equals(""))) {
try {
time = Integer.parseInt(et_time.getText().toString());
tv_time.setText(et_time.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "时间格式不正确", Toast.LENGTH_SHORT).show();
return;
}
}
break;
case R.id.btn_go:
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
mHandler.sendEmptyMessage(MSG_WHAT);
}
}, 0, 1000);
} else {
Toast.makeText(MainActivity.this, "请勿多次点击", Toast.LENGTH_SHORT).show();
return;
}
break;
case R.id.btn_stop:
if (timer != null) {
timer.cancel();
timer = null;
}
break;
default:
break;
}
}
}
好了,教程到此结束。如果此文章帮到了你,欢迎点赞。
源代码需要的可自行下载:
(AndroidStudio版)github下载地址:
https://github.com/QQ986945193/DavidTimerDown
(AndroidStudio版)Git开源中国下载地址:
http://git.oschina.net/MCXIAOBING/DavidTimerDown
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
Android开发小功能,倒计时的实现。时间计时器倒计时功能。
Android应用源码之带暂停功能倒计时TimeCountDown盒子适用
Android RecycleView全部item倒计时的高效实现
用Android Studio编写的有时分秒的计时器,按钮“开始”和“停止”
android倒计时器的简单实现,利用CountDownTimer类。
Android倒计时是一种非常常见的功能,它可以在很多应用中被使用,比如倒计时闹钟、计时器、倒计时游戏等等。它的研究背景可以追溯到计时和倒计时的概念,这些概念和需求是人类社会生活中不可或缺的一部分。在数字化...
本项目是一个集成闹钟,倒计时,秒表等功能的安卓闹钟应用源码,测试了一下项目界面美观大方,功能完整无bug
android倒计时器的demo,采用CountDownTimer
附带三种计时器实现,第三种是最好的。前两种是调研的中间结果,直到发现第三种。
Android计时器实现
Android倒计时器,支持开始倒计时、暂停倒计时、继续倒计时、停止倒计时、重新倒计时
一个促销倒计时的计时器,显示天时分秒,类似于购物网站上的计时器
android项目 android 秒表 android计时器
非常好看的倒计时圆形控件,完全继承view实现,非常实用的特效
一个Android上的倒计时 代码简洁 利用线程完成
Android 秒表倒计时源码.zip
Android实现一个计时器。功能:计时,可以暂停计时。主要利用handler的postdelay()函数。
主要为大家详细介绍了Android利用Chronometer实现倒计时功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
NULL 博文链接:https://manying.iteye.com/blog/985780
一个很简单计时器实现,只有开始和结束按键,以及显示秒。需要用可以下载,根据自已项目随便改就适用。