用Android stdio写一个计时器,用三个按钮代表开始、暂停、重置,当按开始时,有通知开始计时;当按暂停时,有通知暂停计时;当按重置时,有通知重置计时,写出Java代码和xml代码,及具体步骤
时间: 2024-03-14 07:46:32 浏览: 63
好的,以下是用Android Studio编写计时器的具体步骤和Java代码、xml代码:
1. 创建一个新的Android项目,并在布局文件中添加一个TextView用于显示计时器的时间,三个Button分别代表开始、暂停、重置计时器的功能。xml代码如下:
```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00:00"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"/>
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:layout_below="@id/tv_timer"
android:layout_marginTop="32dp"
android:layout_alignParentStart="true"
android:onClick="startTimer"/>
<Button
android:id="@+id/btn_pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:layout_below="@id/tv_timer"
android:layout_marginTop="32dp"
android:layout_centerHorizontal="true"
android:onClick="pauseTimer"/>
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重置"
android:layout_below="@id/tv_timer"
android:layout_marginTop="32dp"
android:layout_alignParentEnd="true"
android:onClick="resetTimer"/>
</RelativeLayout>
```
2. 在MainActivity中定义计时器的变量以及开始、暂停、重置计时器的方法。Java代码如下:
```
public class MainActivity extends AppCompatActivity {
private int seconds = 0;
private boolean running = false;
private boolean wasRunning = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
seconds = savedInstanceState.getInt("seconds");
running = savedInstanceState.getBoolean("running");
wasRunning = savedInstanceState.getBoolean("wasRunning");
}
runTimer();
}
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putInt("seconds", seconds);
savedInstanceState.putBoolean("running", running);
savedInstanceState.putBoolean("wasRunning", wasRunning);
}
public void startTimer(View view) {
running = true;
}
public void pauseTimer(View view) {
running = false;
}
public void resetTimer(View view) {
running = false;
seconds = 0;
}
private void runTimer() {
final TextView tvTimer = (TextView) findViewById(R.id.tv_timer);
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60;
int secs = seconds % 60;
String time = String.format("%02d:%02d:%02d", hours, minutes, secs);
tvTimer.setText(time);
if (running) {
seconds++;
}
handler.postDelayed(this, 1000);
}
});
}
protected void onResume() {
super.onResume();
if (wasRunning) {
running = true;
}
}
protected void onPause() {
super.onPause();
wasRunning = running;
running = false;
}
}
```
3. 在AndroidManifest.xml文件中添加以下权限:
```
<uses-permission android:name="android.permission.VIBRATE" />
```
以上就是用Android Studio编写计时器的全部步骤和代码。
阅读全文