运行效果
在xml的代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<TextView
android:id="@+id/a"
android:layout_width="150dp"
android:layout_height="100dp"
android:textSize="30dp"
android:text="开始计时">
</TextView>
<Chronometer
android:id="@+id/chronometer"
android:layout_below="@+id/a"
android:layout_width="200dp"
android:layout_height="100dp"
android:textColor="#1243D5"
android:textSize="50dp"
/>
</RelativeLayout>
在Java文件的代码
package com.example.jishiqi;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Chronometer;
public class MainActivity extends AppCompatActivity {
Chronometer ch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏显示
ch=findViewById(R.id.chronometer);
ch.setBase(SystemClock.elapsedRealtime());
ch.setFormat("%S");
ch.start();
ch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
if(SystemClock.elapsedRealtime()-ch.getBase()>=60000){
ch.stop();
}
}
});
}
}