as版本:23.3.1patch2
例程:timer
在前一个版本的基本上改的,增加了继续的功能,实现方法稍微不同。
动画演示:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="144dp"
android:layout_marginTop="60dp"
android:text="清零"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_stop" />
<TextView
android:id="@+id/tv_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.272" />
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="80dp"
android:text="开始"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_timer" />
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="暂停"
app:layout_constraintStart_toEndOf="@+id/btn_start"
app:layout_constraintTop_toTopOf="@+id/btn_start" />
<Button
android:id="@+id/btn_resume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="继续"
app:layout_constraintStart_toEndOf="@+id/btn_stop"
app:layout_constraintTop_toTopOf="@+id/btn_stop" />
</androidx.constraintlayout.widget.ConstraintLayout>
mainactivity.java
package com.shudu.timer;
//按钮控制显示计时器。
//开始、暂停、继续、清零四个按钮
//2024.12.30
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
private TextView tvTimer;
private Button btnStart, btnPause, btnResume,btnClear;
private Handler handler = new Handler();
private long startTime=0;
private long elapsedTime = 0;
private Runnable timerRunnable;
private boolean isRunning = false;
private boolean isPaused = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
tvTimer = findViewById(R.id.tv_timer);
btnStart = findViewById(R.id.btn_start);
btnPause = findViewById(R.id.btn_stop);
btnResume = findViewById(R.id.btn_resume);
btnClear=findViewById(R.id.btn_clear);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearTimer();
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isRunning) {
startTime = System.currentTimeMillis();
isRunning = true;
handler.postDelayed(updateTimer, 0);
}
}
});
btnPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isRunning &&!isPaused) {
isPaused = true;
elapsedTime += System.currentTimeMillis() - startTime;
handler.removeCallbacks(updateTimer);
}
}
});
btnResume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPaused) {
isPaused = false;
startTime = System.currentTimeMillis();
handler.postDelayed(updateTimer, 0);
}
}
});
}
private void clearTimer() {
isRunning = false;
// startTime=0;
startTime = System.currentTimeMillis();//这行代码什么意思,不写还不行。写成starttime=0也不行。
tvTimer.setText("00:00:00");
}
private Runnable updateTimer = new Runnable() {
@Override
public void run() {
long currentTime;
if (isRunning) {
if (isPaused) {
currentTime = elapsedTime;
} else {
currentTime = System.currentTimeMillis() - startTime + elapsedTime;
}
int seconds = (int) (currentTime / 1000);
int minutes = seconds / 60;
int hours = minutes / 60;
seconds %= 60;
minutes %= 60;
tvTimer.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds));
handler.postDelayed(this, 1000);
}
}
};
}