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



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



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <TextView
        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" />

    <LinearLayout 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">

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

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

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

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






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<String> 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);
    }
}



  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值