
目录
第一篇:Android应用开发笔记之Android Studio第一个程序(一)
第二篇:Android应用开发笔记之线性布局LinearLayout(二)
第三篇:Android应用开发笔记之线性布局LinearLayout(二)小练习
第四篇:Android应用开发笔记之相对布局RelativeLayout(三)
第五篇:Android应用开发笔记之绘图[上](四)
第五篇:Android应用开发笔记之绘图[下](四)
第六篇:Android应用开发笔记之绘图(四)小练习[画心]
第七篇:Android应用开发笔记之RadioGroup控件(五)
第八篇:Android应用开发笔记之ImageView使用(六)
第九篇:Android应用开发笔记之显示时间(TextClock和AnalogClock)(七)
第十篇:[Android应用开发笔记之计时器(八)(https://www.jianshu.com/p/d48ea387120c)
chronometer
计时器chronometer继承自TextView,显示的是从一个起始时间开始,一共过去了多长时间。
属性只有一个format,用于指定计时器的计时格式。
xml
文件中的形式:
<Chronometer
android:id="@+id/test"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="12pt"
android:textColor="#ffff0000"/>
对应的API
Chronometer的用法很简单,它支持如下用法:
函数 | 描述 |
---|---|
getBase() | 返回时间。 |
setBase(long base) | 设置计时器的起始时间。 |
start() | 开始计时。 |
stop() | 停止计时。 |
setFormat(String format) | 设置显示时间的格式。 |
setOnChronometerTickListener(Chronometer.OnChronometerTickListener listener) | 为计时器绑定监听事件。 |
实战
xml
文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="178dp"
android:layout_marginLeft="178dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="475dp"
android:focusable="true"
android:textColor="#ff0303"
android:textSize="12pt"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/start"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="463dp"
android:text="启动"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/chronometer"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
代码文件:
package com.example.user.stopwatch;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Chronometer;
import android.view.View;
import android.os.SystemClock;
public class MainActivity extends AppCompatActivity {
Chronometer ch ;
Button start ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取计时器组件
ch=(Chronometer)findViewById(R.id.chronometer);
// 获取开始组件
start = (Button)findViewById(R.id.start);
// 开始按钮的响应函数
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//设置开始计时时间
ch.setBase(SystemClock.elapsedRealtime());
//启动计时器
ch.start();
//按钮无效
start.setEnabled(false);
}
});
//计时器绑定监听事件
ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener()
{
@Override
public void onChronometerTick(Chronometer ch)
{
// 如果从开始计时到现在超过了60s
if (SystemClock.elapsedRealtime() - ch.getBase() > 60 * 1000)
{
ch.stop();
start.setEnabled(true);
}
}
});
}
}
执行效果


注意:计时器的效果是累加计时器,倒计时效果可以设定,不过貌似只支持android N以上版本.
网友评论