Android倒计时

感谢极客学院的视频

布局文件:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.timer.MainActivity" >

    <EditText 
        android:id="@+id/inputtime"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        
        />
    
    <Button 
        android:id="@+id/settime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="set"
        />
    
    <TextView
        android:id="@+id/showtime"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        >
        
        <Button
            android:id="@+id/starttime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="start"
            />
        
        <Button
            android:id="@+id/endtime"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="end"
            />
        
    </LinearLayout>

</LinearLayout>

下面是Java文件:

package com.example.timer;

import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity implements OnClickListener{

	EditText inputtime;
	Button settime,starttime,endtime;
	TextView showtime;
	private int i = 0 ;
	Timer timer = null;
	TimerTask task = null;

	private void init(){
		inputtime = (EditText)findViewById(R.id.inputtime);
		settime = (Button)findViewById(R.id.settime);
		starttime = (Button)findViewById(R.id.starttime);
		endtime = (Button)findViewById(R.id.endtime);
		showtime = (TextView)findViewById(R.id.showtime);
		settime.setOnClickListener(this);
		starttime.setOnClickListener(this);
		endtime.setOnClickListener(this);
	}

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

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.settime:
			showtime.setText(inputtime.getText().toString());
			i = Integer.parseInt(inputtime.getText().toString());
			break;
		case R.id.starttime:
			startTime();
			break;
		case R.id.endtime:
			endTime();
			break;
		}
	}
	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg){
			showtime.setText(msg.arg1+"");
			startTime();
		}
	};

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

			@Override
			public void run() {
				// TODO Auto-generated method stub
				i--;
				Message message = mHandler.obtainMessage();
				message.arg1 = i;
				mHandler.sendMessage(message);
			}

		};
		timer.schedule(task,1000);//第二个参数表示以1000毫秒计时
	}

	public void endTime(){
		timer.cancel();
	}
}
  1. 设置监听可以implements OnClickListener接口然后重写onClick(View v)方法,然后给组件widget.setOnClickListener(this);然后在onClick()方法里抓取v.getId()
  2. TextView.setText(xxxx)里xxxx必须是字符串类型否则崩溃,可以用xxx.toString()也可以xxx+""自动转化
时间: 2024-07-28 19:42:46

Android倒计时的相关文章

Android倒计时功能的实现(CountDownTimer)

以前编程的时候,遇到倒计时的功能时,经常自己去写,但其实Android已经帮封装好了一个倒计时类CountDownTimer,其实是将后台线程的创建和Handler队列封装成为了一个方便的类调用. 说明: CountDownTimer timer = new CountDownTimer(30000, 1000)中,第一个参数表示总时间,第二个参数表示间隔时间. 意思就是每隔一秒会回调一次方法onTick,然后30秒之后会回调onFinish方法. package com.androidcoun

Android -- 倒计时的实现

CountDownTimer                                                                      CountDownTimer这个类,实现了倒计时的功能.将后台线程的创建和Handler队列封装成一个方便的类调用. 这个类比较简单,只有四个方法:onTick,onFinsh.cancel和start.其中前面两个是抽象方法,所以要重写一下. 下面是官方给的一个小例子: new CountdownTimer(30000, 100

android倒计时(整理)

android倒计时 用到CountDownTimer Android中文API(143) —— CountDownTimer 前言 本章内容android.os.CountDownTime章节,版本为Android 4.0 r1,翻译来自:"liliang1222",再次感谢他 !期待你一起参与翻译Android的相关资料,联系我[email protected]. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com/ Android中文翻

Android倒计时电子钟的实现(下篇)

本篇继续上篇未完成的部分,这篇文章会实现动态效果如图所示 在程序中先给定一个截止时间: 2014-12-20 00:00:00 然后计算当期时间和截止时间相差的时间 private long getCurrentShowTimeSeconds() { Date curTime = new Date(); long ret = endDate.getTime() - curTime.getTime(); ret =Math.round( ret/1000 ); return ret >= 0 ?

Android 倒计时动画

今天给大家分享一个可以用来做倒计时的动画....下期开始准备写几篇动画的详细解释,毕竟授人以鱼不如授人以渔 接下来还是先上效果图 熟悉动画的人相信心中已经有了想法..其实这个动画就是Alpha动画和Scale动画结合在一起 <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="1000" android:fromAlpha

android 倒计时实现

新的商品详情,对特卖要增加倒计时的功能: 采用了android系统的CountDownTimer这个类 关于这个类的用法: * Schedule a countdown until a time in the future, with * regular notifications on intervals along the way. * * Example of showing a 30 second countdown in a text field: * * <pre class="

Android倒计时效果

借用聚美优品的广告词来开始今天的文章之旅: 从未年轻过的人,一定无法体会这个世界 的偏见.我们被世俗拆散,也要为爱情勇 往直前:我们被房价羞辱,也要让简陋的 现实变的温暖:我们被权威漠视,也要为 自己的天分保持骄傲:我们被平庸折磨, 也要开始说走就走的冒险.所谓的光辉岁 月,并不是后来闪耀的日子,而是无人问 津时,你对梦想的偏执,你是否有勇气, 对自己忠诚到底,我是Bruce常,我为自己加油. 平常开发中,在做倒计时效果的时候,经常需要用到定时器,今天看到一篇文章,专门写 定时器的,我就仔细阅读

Android 倒计时工具类

在平时我们编程的时候,经常会用到倒计时这个功能,很多人不知道Android已经帮封装好了一个类,往往都自己写.现在发现了这个类,大家共享一下: 在一个TextView不断显示剩下的时间,代码如下: [java] view plaincopy private TextView vertifyView; private CountDownTimer timer = new CountDownTimer(10000, 1000) { @Override public void onTick(long 

Android倒计时CountDownTimer小记

Android 超简便的倒计时实现:  CountDownTimer CountDownTimer由系统提供 查资料的时候 发现了CountDownTimer这个类之后 果断抛弃了以前的倒计时做法 功能: 30秒倒计时 每次间隔1秒 参数: mc.start();方法开始 mc.cancel();方法结束 new MyCountDownTimer(30000, 1000); 第一个参数表示 总的时间为30000毫秒,间隔1000毫秒 直接上代码: package com.example.daoj

Android倒计时电子钟的实现(上篇)

习惯了Android的默认倒计时字体,这里采用Canvas画图的方式实现倒计时时钟效果,首先先看一下静态效果 不难发现,每个数字都是一个小圆形,也可以改成一个小正方形等等,这些都是可以实现的. 这里使用圆形. 仔细观察数字发现其是由一个二维数组组成,类似如下所示:根据下面数组可以画出0的,效果 {0,0,1,1,1,0,0}, {0,1,1,0,1,1,0}, {1,1,0,0,0,1,1}, {1,1,0,0,0,1,1}, {1,1,0,0,0,1,1}, {1,1,0,0,0,1,1},