最最基础的Android倒计时应用

只精确到秒

activity_main.xml

<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="com.example.counttime.MainActivity" >

    
    
    <EditText
        android:id="@+id/inputtime"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number"
        android:maxLength="9"
        >

        <requestFocus />
        
    </EditText>
    
    <Button
        android:id="@+id/gettime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取倒计时时间" />

    <TextView
        android:id="@+id/time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="40sp"
        android:textColor="#0000FF"
        android:gravity="center"
         />

    <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 com.example.counttime;

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;

public class MainActivity extends Activity implements OnClickListener{

	private EditText inputet;
	
	private Button getTime,startTime,stopTime;
	private TextView time;
	private int i = 0;

	private Timer timer;
	private TimerTask task = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}
	
	private void initView(){
		inputet = (EditText) findViewById(R.id.inputtime);
		getTime = (Button) findViewById(R.id.gettime);
		startTime = (Button) findViewById(R.id.starttime);
		stopTime = (Button) findViewById(R.id.stoptime);
		time = (TextView) findViewById(R.id.time);
		
		getTime.setOnClickListener(this);
		startTime.setOnClickListener(this);
		stopTime.setOnClickListener(this);
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.gettime:
			if(inputet.getText().toString().length()>0){
				stopTime();
				time.setText(inputet.getText().toString());
				i = Integer.parseInt(inputet.getText().toString());
			}
			
			break;

		case R.id.starttime:
			startTime();
			break;
		
		case R.id.stoptime:
			stopTime();
			break;
		}
	}
	
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			if(time.length()>0){
				time.setText(msg.arg1+"");
				if(!time.getText().toString().equals("0")){
					startTime();
				}
			}
		};
	};
	
	public void startTime(){
		timer = new Timer();
		task = new TimerTask() {
			
			@Override
			public void run() {
				i--;
				Message message = handler.obtainMessage();
				message.arg1 = i;
				handler.sendMessage(message);
			}
		};
		timer.schedule(task, 1000);
	}
	
	public void stopTime(){
		if(timer == null){
			timer = new Timer();
		}
		timer.cancel();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值