Androidstudio计时器源代码
时间: 2023-08-03 12:04:06 浏览: 182
android计时器源代码
5星 · 资源好评率100%
以下是一个简单的计时器应用程序的源代码:
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
private TextView timerTextView;
private Button startButton, pauseButton, resetButton;
private CountDownTimer countDownTimer;
private boolean timerRunning;
private long startTimeInMillis, timeLeftInMillis;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerTextView = findViewById(R.id.timerTextView);
startButton = findViewById(R.id.startButton);
pauseButton = findViewById(R.id.pauseButton);
resetButton = findViewById(R.id.resetButton);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!timerRunning) {
startTimer();
}
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timerRunning) {
pauseTimer();
}
}
});
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetTimer();
}
});
}
private void startTimer() {
countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftInMillis = millisUntilFinished;
updateTimer();
}
@Override
public void onFinish() {
timerRunning = false;
updateButtons();
}
}.start();
timerRunning = true;
updateButtons();
}
private void pauseTimer() {
countDownTimer.cancel();
timerRunning = false;
updateButtons();
}
private void resetTimer() {
timeLeftInMillis = startTimeInMillis;
updateTimer();
updateButtons();
}
private void updateTimer() {
int minutes = (int) (timeLeftInMillis / 1000) / 60;
int seconds = (int) (timeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
timerTextView.setText(timeLeftFormatted);
}
private void updateButtons() {
if (timerRunning) {
startButton.setEnabled(false);
pauseButton.setEnabled(true);
resetButton.setEnabled(false);
} else {
startButton.setEnabled(true);
pauseButton.setEnabled(false);
resetButton.setEnabled(true);
}
}
}
```
activity_main.xml:
```xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.timer.MainActivity">
<TextView
android:id="@+id/timerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="00:00"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/startButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_below="@+id/timerTextView"
android:layout_marginTop="16dp"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_toLeftOf="@+id/resetButton"
android:layout_below="@+id/timerTextView"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:enabled="false"/>
<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_alignParentRight="true"
android:layout_below="@+id/timerTextView"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:enabled="false"/>
</RelativeLayout>
```
这个应用程序包含一个 `TextView` 显示剩余时间,以及三个按钮:`Start`,`Pause` 和 `Reset`。当用户点击 `Start` 按钮时,计时器开始倒计时,并且 `Start` 按钮禁用,`Pause` 按钮启用。当用户点击 `Pause` 按钮时,计时器暂停,并且 `Pause` 按钮禁用,`Start` 按钮启用。当用户点击 `Reset` 按钮时,计时器被重置为初始状态,即未开始状态。
阅读全文