实现功能:
- 创建一个Activity,启动后在其中心显示一个简单的数字秒表;
- 创建一个后台服务,开启新线程,在新线程中每隔1秒,控制界面上的秒表数字增1;
- 每点击界面上的“快进”按钮一次,内部通过前面创建的后台服务,将秒表数字增10;
- 点击界面上的“停止”按钮,停止服务及新线程,从而停止秒表更新。
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginTop="500px"
android:layout_gravity="center_horizontal"
android:textSize="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/start"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="50dp"
android:layout_marg

本文介绍了如何在Android项目中实现一个数字秒表,包括创建Activity显示秒表、后台服务开启新线程每秒更新计数、点击快进按钮加速计数以及停止按钮关闭服务。在开发过程中,遇到了Resources$NotFoundException和秒表重置问题,并提供了相应的解决方案:将数据显示为字符串以及在停止后再启动时保存并继续计数。
最低0.47元/天 解锁文章
2812

被折叠的 条评论
为什么被折叠?



