今天看别人项目发现一个有趣的东西,计时器,自己也试着写了两个简单的用用
1、android 自带的chronometer(自带的就简单了,二话不说,上代码)
activity_main.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xiaohua.android.chronometertest.MainActivity">
<Chronometer
android:id="@+id/chron"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="80sp"
android:format="%s"
android:textColor="#00ff00"
android:gravity="center"/>
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="start"
android:onClick="click"/>
<Button
android:id="@+id/stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stop"
android:onClick="click"/>
<Button
android:id="@+id/restart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="restart"
android:onClick="click"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private Chronometer chronometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chronometer = (Chronometer) findViewById(R.id.chron);
}
public void click(View v){
switch (v.getId()){
case R.id.start:
chronometer.start();
break;
case R.id.stop:
chronometer.stop();
break;
case R.id.restart:
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
break;
}
}
}
点击start按钮开始显示计时(不过他这个计时器好像是从chronometer一创建就开始计时,只不过是在点击start之后数字才开始变化),点击stop停止计时(此时只不过是数字停止变化,但是时间还是继续走呢,再次点击start中间隔了多长时间,就会直接跳过这段时间,原因……待进一步研究),点击restart从0重新计时,来张图片:
2、使用Timer和TimerTask来计时(要使用这种方法首先要了解Timer和TimerTask,这里就不细讲了,之后看代码就so easy了):
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.xiaohua.android.timertest.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="28sp"
android:gravity="center"
android:text="0" />
<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="开始"/>
<Button
android:id="@+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
android:onClick="click"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private Timer timer;
private TimerTask timerTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView tv = (TextView) findViewById(R.id.tv);
timer = new Timer();
timerTask = new TimerTask() {
int num = 0;
@Override
public void run() {
runOnUiThread(new Thread(){
public void run(){
tv.setText(""+(num++));
}
});
}
};
}
public void click(View v){
switch (v.getId()){
case R.id.start:
timer.schedule(timerTask,500,1000);
break;
case R.id.cancel:
if(!timerTask.cancel()){
timer.cancel();
timerTask.cancel();
}
break;
}
}
}
点击按钮开始,开始计时;点击取消,停止计时
上图片(简单暴力):