Android 计时器 分:秒:毫秒 http://download.csdn.net/detail/tangjili5620/9876529



http://download.csdn.net/detail/tangjili5620/9876529


Android 计时器 分:秒:毫秒 http://download.csdn.net/detail/tangjili5620/9876529_第1张图片
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.demo.mytime.MainActivity">

            android:id="@+id/tvTime"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/init_time_100millisecond"
        android:textSize="21sp" />

    xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

                    android:id="@+id/btnStartPaunse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/start"
            android:textSize="20sp" />

                    android:id="@+id/btnStart"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/pause"
            android:textSize="20sp" />

                    android:id="@+id/btnStop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/stop"
            android:textSize="20sp" />
                    android:id="@+id/btnadd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/add"
            android:textSize="20sp" />
                    android:id="@+id/btndel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/delect"
            android:textSize="20sp" />
    

            android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />







package com.demo.mytime;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    protected ImageButton btnStartPaunse;
    protected ImageButton btnStart;
    protected LinearLayout activityMain;
    protected ListView list;
    protected ImageButton btnadd;
    protected ImageButton btndel;
    private long mlCount = 0;
    private long mlTimerUnit = 10;
    private TextView tvTime;
    private ImageButton btnStop;
    private Timer timer = null;
    private TimerTask task = null;
    private Handler handler = null;
    private Message msg = null;
    private boolean bIsRunningFlg = false;
    private int yushu = 0;
    private int min = 0;
    private int sec = 0;
    private int totalSec = 0;

    private List times = new ArrayList<>();
    private MyAdaptere adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_main);
        initView();
        tvTime.setText(R.string.init_time_100millisecond);
        // Handle timer message
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                switch (msg.what) {
                    case 1:
                        mlCount++;
                        totalSec = 0;
                        // 100 millisecond
                        totalSec = (int) (mlCount / 100);
                        yushu = (int) (mlCount % 100);
                        // Set time display
                        min = (totalSec / 60);
                        sec = (totalSec % 60);
                        try {
                            // 100 millisecond
                            tvTime.setText(String.format("%1$02d:%2$02d:%3$d", min, sec, yushu));
                        } catch (Exception e) {
                            tvTime.setText("" + min + ":" + sec + ":" + yushu);
                            e.printStackTrace();
                            Log.e("MyTimer onCreate", "Format string error.");
                        }
                        break;

                    default:
                        break;
                }

                super.handleMessage(msg);
            }

        };
    }

    @Override
    public void onClick(View view) {
        // Start and pause
        if (view.getId() == R.id.btnStartPaunse) {
            if (null == timer) {
                if (null == task) {
                    task = new TimerTask() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (null == msg) {
                                msg = new Message();
                            } else {
                                msg = Message.obtain();
                            }
                            msg.what = 1;
                            handler.sendMessage(msg);
                        }

                    };
                }
                timer = new Timer(true);
                timer.schedule(task, mlTimerUnit, mlTimerUnit); // set timer duration
            }
            // 清除
        } else if (view.getId() == R.id.btnStop) {
            if (null != timer) {
                task.cancel();
                task = null;
                timer.cancel(); // Cancel timer
                timer.purge();
                timer = null;
                handler.removeMessages(msg.what);
            }
            mlCount = 0;
            // 100 millisecond
            tvTime.setText(R.string.init_time_100millisecond);
        } else if (view.getId() == R.id.btnStart) {
            Toast.makeText(MainActivity.this, "try   " + mlCount + "   " + String.format("%1$02d:%2$02d:%3$d", min, sec, yushu), Toast.LENGTH_SHORT).show();
            //暂停
            if (null == timer) {
                if (null == task) {
                    task = new TimerTask() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            if (null == msg) {
                                msg = new Message();
                            } else {
                                msg = Message.obtain();
                            }
                            msg.what = 1;
                            handler.sendMessage(msg);
                        }

                    };
                }
                timer = new Timer(true);
                timer.schedule(task, mlTimerUnit, mlTimerUnit); // set timer duration
            }
            try {
                bIsRunningFlg = false;
                task.cancel();
                task = null;
                timer.cancel(); // Cancel timer
                timer.purge();
                timer = null;
                handler.removeMessages(msg.what);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (view.getId() == R.id.btnadd) {
            //添加数据
            times.add(String.format("%1$02d:%2$02d:%3$d", min, sec, yushu));
            adapter.notifyDataSetChanged();
        } else if (view.getId() == R.id.btndel) {
            //清空数据
            times.clear();
            adapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        tvTime = (TextView) findViewById(R.id.tvTime);
        btnStartPaunse = (ImageButton) findViewById(R.id.btnStartPaunse);
        btnStartPaunse.setOnClickListener(MainActivity.this);
        btnStop = (ImageButton) findViewById(R.id.btnStop);
        btnStop.setOnClickListener(MainActivity.this);
        btnStart = (ImageButton) findViewById(R.id.btnStart);
        btnStart.setOnClickListener(MainActivity.this);
        activityMain = (LinearLayout) findViewById(R.id.activity_main);
        list = (ListView) findViewById(R.id.list);

        adapter = new MyAdaptere(times, this);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, times.get(position) + "", Toast.LENGTH_SHORT).show();
            }
        });
        btnadd = (ImageButton) findViewById(R.id.btnadd);
        btnadd.setOnClickListener(MainActivity.this);
        btndel = (ImageButton) findViewById(R.id.btndel);
        btndel.setOnClickListener(MainActivity.this);
    }
}



你可能感兴趣的:(Android)