android---简单的计时器

android–简单的计时器


mainactivity

package com.example.time;

import java.util.Timer;
import java.util.TimerTask;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements android.view.View.OnClickListener{

    private EditText inputTime;
    private Button getTime,startTime,stopTime;
    private int i = 0;
    private TextView time;
    private Timer timer = null;
    private TimerTask timerTask = null;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {//专门使用一个方法来初始化ID
        inputTime = (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 (inputTime.getText().toString().equals("")) {
            Toast toast = Toast.makeText(getApplicationContext(), "请输入倒计时时间",Toast.LENGTH_SHORT);
            toast.show();
            }else{
                time.setText(inputTime.getText().toString());
                i = Integer.parseInt(inputTime.getText().toString());
            }
            break;
        case R.id.startTime:
            start();
            break;
        case R.id.stopTime:
            stop();
            break;
        }
    }
    /*
     * handler创建一个匿名内部类,然后重写handleMessage方法用来得到传入的值
     */
    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            if(msg.arg1!=-1){//判断倒计时是否已经结束
                time.setText(msg.arg1+"");
                start();
            }else{
                Toast toast = Toast.makeText(getApplicationContext(), "倒计时结束",Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    };

    public void start() {
        //利用timer计时器创建子线程,从而保证主线程不会卡死,延迟
        timer = new Timer();
        timerTask = new TimerTask() {

            public void run() {
                i--;
                Message message = handler.obtainMessage();
                message.arg1 = i;
                handler.sendMessage(message);

            }
        };
        timer.schedule(timerTask, 1000);//执行timer计时器
    }

    public void stop() {
        timer.cancel();//计时器停止
    }
}

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.time.MainActivity" >

    <EditText
        android:id="@+id/inputtime"
        android:layout_width="match_parent"
        android:inputType="number"
        android:layout_height="wrap_content"
        android:ems="10" >
    </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="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>

下一篇博客会详细讲述遇到的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-06-23 23:22  牛李  阅读(252)  评论(0)    收藏  举报

导航