上图
主代码
package com.example.tuya;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private Button bt_clean;
private LinearLayout ll;
private Button btBegin;
private Chronometer chronometer;
private boolean isRun = false;
private long recordingTime = 0;// 记录下来的总时间
//程序首先进入onCreate方法中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//布局加载进来
setContentView(R.layout.activity_main);
//隐藏标题
getSupportActionBar().hide();
//初始化id
ll = (LinearLayout) findViewById(R.id.ll);
btBegin = (Button) findViewById(R.id.btn_begin);
bt_clean = (Button) findViewById(R.id.bt_clean);
chronometer = new Chronometer(this);
//设置openAndClose按钮的点击事件
btBegin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//用一个boolean变量 让按钮有两个功能
if (isRun) {
//调用暂停的方法
onRecordPause();
} else {
//每次都new 出来新对象并且设置上去
chronometer = new Chronometer(getApplicationContext());
chronometer.setTextSize(24);
chronometer.setTextColor(Color.RED);
chronometer.setGravity(Gravity.CENTER);
//设置chronometer控件的格式
int hour = (int) ((SystemClock.elapsedRealtime() - chronometer.getBase()) / 1000 / 60);
chronometer.setFormat("0" + String.valueOf(hour) + ":%s");
//调用开始的方法
onRecordStart();
ll.addView(chronometer);
}
isRun = !isRun;
}
});
//清空操作
bt_clean.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//清空操作 直接移除所有的view
ll.removeAllViews();
onRecordStop();
}
});
}
//记录时间的开始
public void onRecordStart() {
chronometer.setBase(SystemClock.elapsedRealtime() - recordingTime);// 跳过已经记录了的时间,起到继续计时的作用
chronometer.start();
}
//记录时间的暂停
public void onRecordPause() {
chronometer.stop();
recordingTime = SystemClock.elapsedRealtime() - chronometer.getBase();// 保存这次记录了的时间
}
//方法的停止
public void onRecordStop() {
recordingTime = 0;
//计时器 清零
chronometer.setBase(SystemClock.elapsedRealtime());
}
}
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg"
android:gravity="center"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="500dp">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="500dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btn_begin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="开始/暂停计时" />
<Button
android:id="@+id/bt_clean"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="7dp"
android:text="清空" />
</LinearLayout>