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



更多相关文章

  1. android中listview分批加载数据
  2. Android本地数据搜索实现
  3. Android Timer(计时器)
  4. Android studio连接Bmob云数据库教程
  5. Android清除本地数据缓存代码案例
  6. android通过httpClient请求获取JSON数据并且解析
  7. android nosql 数据库对比 Realm vs ObjectBox
  8. Android-TCPDump for Android(抓TCP数据包工具)
  9. Android移动终端数据同步技术收集贴

随机推荐

  1. Android(安卓)程式开发:(十)基本控件 —— 1
  2. [Android(安卓)NDK]添加C++11和C++14支持
  3. Android(安卓)API Demo实例解析
  4. android布局之线性布局(LinearLayout)
  5. Android(安卓)PackageManagerService(二)下
  6. android 中关于SimpleAdapter构造参数的
  7. ListView的item高度调整
  8. Android根据Url显示gif类型图片
  9. Android(安卓)骁龙Camera拍照流程梳理
  10. Android:为什么声明控件和控件赋值要分开