Chronometer

简介

安卓原生计时器控件,默格式00:00(MM:SS)

属性

格式(官方提供两方式)

  • 布局android:format
  • 代码setFormat()

方法

开始

start();

停刷(仍在计时)

stop();

初始值

setBase(long base);

顺计时

setCountDown(false);

倒计时

setCountDown(true);

监听

Chronometer chronometer;
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    @Override
    public void onChronometerTick(Chronometer cArg) {
        long time =System.currentTimeMillis() - cArg.getBase();
        Date d = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        timeView.setText(sdf.format(d));
    }
});
timeView.setBase(System.currentTimeMillis());
timeView.start();
Chronometer chronometer;
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    @Override
    public void onChronometerTick(Chronometer cArg) {
        long time =System.currentTimeMillis() - cArg.getBase();
        int h = (int) (time / 3600000);
        int m = (int) (time - h * 3600000) / 60000;
        int s = (int) (time - h * 3600000 - m * 60000) / 1000;
        String hh = h < 10 ? "0" + h : h + "";
        String mm = m < 10 ? "0" + m : m + "";
        String ss = s < 10 ? "0" + s : s + "";
        String timeFormat = hh + ":" + mm + ":" + ss;
        meetingView.updateTime(timeFormat);
        timeView.setText(sdf.format(d));
    }
});
timeView.setBase(System.currentTimeMillis());
timeView.start();

循环隔一秒操作

Chronometer chronometer;
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
    @Override
    public void onChronometerTick(Chronometer chronometer) {
        if (mRecordPromptCount == 0) {
            textView.setText(getString(R.string.record_in_progress) + ".");
        } else if (mRecordPromptCount == 1) {
            textView.setText(getString(R.string.record_in_progress) + "..");
        } else if (mRecordPromptCount == 2) {
            textView.setText(getString(R.string.record_in_progress) + "...");
            mRecordPromptCount = -1;
        }
        mRecordPromptCount++;
    }
});

使用

布局


主代码

// timer zero
chronometer.setBase(SystemClock.elapsedRealtime());
// start Chronometer
chronometer.start();

// stop Chronometer
chronometer.stop();
// timer zero
chronometer.setBase(SystemClock.elapsedRealtime());

你可能感兴趣的:(android)