用eclipse移动应用开发设计一个计时器,包括main.xml文件和java文件
时间: 2024-05-12 22:14:56 浏览: 130
这里提供一个简单的计时器示例,包括一个主界面的布局文件 `activity_main.xml` 和一个 Java 文件 `MainActivity.java`。
1. `activity_main.xml` 文件:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/timerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_centerInParent="true"
android:text="00:00:00" />
<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="20dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="40dp" />
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_below="@+id/timerTextView"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/resetButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_below="@+id/timerTextView"
android:layout_marginTop="20dp"
android:layout_alignParentRight="true"
android:layout_marginRight="40dp" />
</RelativeLayout>
```
2. `MainActivity.java` 文件:
```java
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView timerTextView; // 显示计时的 TextView
private Button startButton, pauseButton, resetButton; // 开始、暂停、重置按钮
private int seconds = 0; // 计时器秒数
private boolean isRunning = false; // 是否已经开始计时
private boolean wasRunning = false; // 计时器是否在暂停前运行
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerTextView = findViewById(R.id.timerTextView);
startButton = findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTimer();
}
});
pauseButton = findViewById(R.id.pauseButton);
pauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pauseTimer();
}
});
resetButton = findViewById(R.id.resetButton);
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetTimer();
}
});
if (savedInstanceState != null) {
seconds = savedInstanceState.getInt("seconds");
isRunning = savedInstanceState.getBoolean("isRunning");
wasRunning = savedInstanceState.getBoolean("wasRunning");
}
runTimer();
}
// 保存状态以防止屏幕旋转时计时器重置
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("seconds", seconds);
savedInstanceState.putBoolean("isRunning", isRunning);
savedInstanceState.putBoolean("wasRunning", wasRunning);
super.onSaveInstanceState(savedInstanceState);
}
// 开始计时
private void startTimer() {
isRunning = true;
}
// 暂停计时
private void pauseTimer() {
isRunning = false;
wasRunning = true;
}
// 重置计时器
private void resetTimer() {
seconds = 0;
isRunning = false;
wasRunning = false;
}
// 计算时间并更新 TextView
private void runTimer() {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int secs = seconds % 60;
String time = String.format("%02d:%02d:%02d", hours, minutes, secs);
timerTextView.setText(time);
}
};
Thread thread = new Thread() {
@Override
public void run() {
while (true) {
if (isRunning) {
try {
Thread.sleep(1000); // 休眠 1 秒钟
} catch (InterruptedException e) {
e.printStackTrace();
}
seconds++;
}
handler.sendEmptyMessage(0);
}
}
};
thread.start();
}
// 如果计时器在暂停前运行,则在恢复时继续计时
@Override
protected void onResume() {
super.onResume();
if (wasRunning) {
isRunning = true;
}
}
// 如果计时器在暂停前运行,则在暂停时停止计时
@Override
protected void onPause() {
super.onPause();
wasRunning = isRunning;
isRunning = false;
}
}
```
以上就是一个简单的计时器的实现。你可以根据自己的需要对布局和代码进行修改和扩展。还需要添加相关的权限和依赖库,才能正常运行计时器应用。
阅读全文