java - 为什么我的定时器重置按钮在定时器应用程序中工作错误?

标签 java android

我是 android 的新手。我认为创建一个像计时器这样的简单应用是一个很好的开始方式。

我设法创建了我的简单计时器应用程序,但遇到了问题。

我让我的应用启动和暂停,但我不知道如何将它重置回“00:00:00”。我在 Java 代码的第 56-59 行尝试了以下内容。

package com.awesomeapps.misael.timer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private Button startButton;
    private Button pauseButton;

    private TextView timerValue;

    private long startTime = 0L;

    private Handler customHandler = new Handler();

    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedTime = 0L;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        timerValue = (TextView) findViewById(R.id.timerValue);

        startButton = (Button) findViewById(R.id.startButton);

        startButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                startTime = SystemClock.uptimeMillis();
                customHandler.postDelayed(updateTimerThread, 0);

            }
        });

        pauseButton = (Button) findViewById(R.id.pauseButton);

        pauseButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {

                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);

            }
        });
    }
    public void onButtonTapReset(View v)
    {
        timerValue.setText("00:00:00");
    }

    private Runnable updateTimerThread = new Runnable() {

        public void run() {

            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            timerValue.setText("" + mins + ":"
                    + String.format("%02d", secs) + ":"
                    + String.format("%03d", milliseconds));
            customHandler.postDelayed(this, 0);
        }

    };

}

当我按下我的重置按钮时,它确实变成了“00:00:00”,但是当我按下我的开始按钮时,计时器从它停止的地方恢复。例如,如果计时器处于“00:05:123”,按下重置键,它将变为“00:00:00”,但是当我再次按下开始时,它会从它停止的地方开始,甚至可能更远一点,比如“00:08:456"当我按下重置然后重新开始时,它不会从零开始。

谁能帮我弄清楚我的重置按钮,好吗?

我附上了一些文件,我认为我的错误可能在于包含上面的 Java 代码。

字符串 xml 文件。

<resources>
    <string name="app_name">Timer</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="timerVal">00:00:00</string>
    <string name="pauseButtonLabel">Pause</string>
    <string name="startButtonLabel">Start</string>
</resources>

activity_main xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/timerValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/pauseButton"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="37dp"
        android:textSize="40sp"
        android:textColor="#ffffff"
        android:text="@string/timerVal" />
    <Button
        android:id="@+id/startButton"
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="38dp"
        android:text="@string/startButtonLabel" />
    <Button
        android:id="@+id/pauseButton"
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:layout_alignBaseline="@+id/startButton"
        android:layout_alignBottom="@+id/startButton"
        android:layout_alignParentRight="true"
        android:layout_marginRight="38dp"
        android:text="@string/pauseButtonLabel" />

    <Button
        android:id="@+id/buttonReset"
        android:layout_width="90dp"
        android:layout_height="45dp"
        android:text="Reset"
        android:layout_below="@+id/timerValue"
        android:layout_centerHorizontal="true"
        android:onClick="onButtonTapReset" />
</RelativeLayout>

我知道我的问题很长,但我喜欢详细点。

最佳答案

当用户单击取消按钮时, View 的文本暂时设置为 00:00:000,因为第二个线程仍在后台更新通过此代码运行的文本:

entprivate Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        timerValue.setText("" + mins + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }

};

您应该考虑在这段代码中点击取消按钮并采取相应行动的可能性。

为了检查是否是这种情况,一个快速的解决方案是在取消按钮的 OnClick 监听器中将 startTime 更改为当前时间。 然后时间应设置在“00:00:000”附近。 如果是这种情况,那么很容易找到一种方法使其完美运行。

关于java - 为什么我的定时器重置按钮在定时器应用程序中工作错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38037708/

相关文章:

java - 为什么枚举常量必须在枚举类型中的任何其他变量和方法声明之前声明?

java - 为什么短类型参数的 Integer.toBinaryString 结果包括 32 位?

java - 如何在java中将二进制字符串转换为十六进制并通过Uart端口发送

JavaFX Print WebView as PDF 仅打印一页

android - OSMDroid main.xml map View 应该如何显示?

java - 阅读并理解 Android 上的 AMR 文件的内容

Android SDK Manager,包的本地副本

java - SFTP 读取目录中的所有文件

android - 从缓存目录通过电子邮件发送文件

android - 在 listView 自定义适配器中获取父 viewHolder