秒表之handle写法

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import java.text.DateFormat;
import java.util.Date;

public class otherActivity extends Activity {

    long time = 0;
    long startTime = 0;
    long pauseTime = 0;
    private TextView textView;
    private Button btn;
    private Button btn1;
    private Button btn2;
    private Handler handler = new Handler();

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        initView();
    }

    public void initView() {
        textView = (TextView) findViewById(R.id.textView2);
        textView.setText("00:00:00:00");
        ImageButton imageButton = (ImageButton) findViewById(R.id.btn4);
        imageButton.setImageResource(R.drawable.img);
        btn = (Button) findViewById(R.id.btn1);
        btn1 = (Button) findViewById(R.id.btn2);
        btn2 = (Button) findViewById(R.id.btn3);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handler.post(freshtime);
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pauseTime = pauseTime + time - startTime;
                startTime = 0;
                handler.removeCallbacks(freshtime);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setText("00:00:00:00");
                startTime = 0;
                pauseTime = 0;
                handler.removeCallbacks(freshtime);
            }
        });
    }

    private Runnable freshtime = new Runnable() {
        @Override
        public void run() {
            time = System.currentTimeMillis();
            if (startTime == 0) {
                startTime = time;
            }

            long hour = ((time - startTime + pauseTime) / 3600000) % 24;
            long mimute = ((time - startTime + pauseTime) / 60000) % 60;
            long seconds = (time - startTime + pauseTime) / 1000 % 60;

            String Time = timeform(hour) + ":" + timeform(mimute) + ":" + timeform(seconds) + ":" + timeform((time - startTime + pauseTime) % 100);
            textView.setText(Time);
            handler.postDelayed(freshtime, 100);

        }
    };

    String timeform(long tt) {

        return tt < 10 ? "0" + tt : String.valueOf(tt);
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值