android基础:handler与messag案例(计时器)

 

运行效果

android基础:handler与messag案例(计时器)

 

代码文件

string.xml

<?xml version="1.0" encoding="utf-8"?>  

<resources>  

    <string name="action_settings">Settings</string>  

    <string name="app_name">计时器</string>  

    <string name="gettime">计时</string>  

    <string name="starttime">开始</string>  

    <string name="stoptime">停止</string>  

    <string name="inputhint">请输入时间</string>  

</resources>  

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.mytest.mycounttime.MainActivity" >  

  

    <EditText  

        android:id="@+id/inputTime"  

        android:layout_width="fill_parent"  

        android:layout_height="wrap_content"  

        android:ems="10"  

        android:hint="@string/inputhint"  

        android:inputType="number" >  

        <requestFocus />  

    </EditText>  

  

    <Button  

        android:id="@+id/getTime"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="@string/gettime" />  

  

    <Button  

        android:id="@+id/startTime"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="@string/starttime" />  

  

    <Button  

        android:id="@+id/stopTime"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="@string/stoptime" />  

  

    <TextView  

        android:id="@+id/timeView"  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content" />  

  

</LinearLayout> 

 

activity.java

package com.mytest.mycounttime;  

  

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.util.Log;  

import android.view.Menu;  

import android.view.MenuItem;  

import android.view.View;  

import android.widget.Button;  

import android.widget.EditText;  

import android.widget.TextView;  

  

public class MainActivity extends Activity {  

  

    private Button getTimeButton, startTimeButton, stopTimeButton;  

    private EditText inputTimeText;  

    private TextView timeViewText;  

  

    private int inputTime;  

    private int currTime;  

    private Timer timer;  

    private TimerTask timerTask;  

    private Handler handler = new MessageHandler();  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

  

        // 页面元素赋值  

        initView();  

  

        getTimeButton.setOnClickListener(new TimeButtonOnClickListener());  

        startTimeButton.setOnClickListener(new TimeButtonOnClickListener());  

        stopTimeButton.setOnClickListener(new TimeButtonOnClickListener());  

  

    }  

  

    private void initView() {  

        getTimeButton = (Button) this.findViewById(R.id.getTime);  

        startTimeButton = (Button) this.findViewById(R.id.startTime);  

        stopTimeButton = (Button) this.findViewById(R.id.stopTime);  

        inputTimeText = (EditText) this.findViewById(R.id.inputTime);  

        timeViewText = (TextView) this.findViewById(R.id.timeView);  

    }  

  

    private final class MessageHandler extends Handler {  

  

        @Override  

        public void handleMessage(Message msg) {  

            // timeViewText.setText(currTime);  

            // 显示当前时间  

            timeViewText.setText("时间:"+msg.arg1 + "");  

            startTime();  

        }  

  

    }  

  

    private final class TimeButtonOnClickListener implements View.OnClickListener {  

  

        @Override  

        public void onClick(View v) {  

            Log.v("info", "currTime:" + currTime);  

  

            switch (v.getId()) {  

            case R.id.getTime:  

                inputTime = Integer.parseInt(inputTimeText.getText().toString());  

                currTime = inputTime;  

                break;  

  

            case R.id.startTime:  

                startTime();  

                break;  

            case R.id.stopTime:  

                stopTime();  

                break;  

  

            }  

  

        }  

  

    }  

  

    private void stopTime() {  

        timer.cancel();  

  

    }  

  

    private void startTime() {  

  

        timer = new Timer();  

        timerTask = new TimerTask() {  

  

            @Override  

            public void run() {  

  

                Log.v("info", "currTime:" + currTime);  

  

                if (currTime <= 0) {  

                    timer.cancel();  

                    return;  

                }  

  

                currTime--;  

  

                // 发送消息,传送当前时间  

                Message message = handler.obtainMessage();  

                message.arg1 = currTime;  

                handler.sendMessage(message);  

            }  

  

        };  

  

        timer.schedule(timerTask, 1000);  

  

    }  

  

}  

  

你可能感兴趣的:(android)