【问题标题】:Android TImer: Add a timerAndroid 计时器:添加计时器
【发布时间】:2014-07-21 13:16:26
【问题描述】:

我一直在尝试使用 Eclipse... 我几乎完成了一个计时器:) 虽然,我无法让“重置”按钮工作......

Res/Layout/main.xml

<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/resetButton"
            android:layout_width="90dp"
            android:layout_height="45dp"
            android:layout_above="@+id/timerValue"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="31dp"
            android:text="@string/resetButtonLabel" />

</RelativeLayout>
[/code]

[B]res/values/strings.xml[/B]
[code]<?xml version="1.0" encoding="utf-8"?>

<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>
    <string name="resetButtonLabel">Reset</string>
</resources>

MainActivity.java

package com.MertensMobile.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.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);
            }
        });

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

        resetButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                timeSwapBuff += timeInMilliseconds;
                customHandler.removeCallbacks(updateTimerThread);
            }
        });

    }
    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);
        }
        public void Reset() {
            Intent intent = getIntent();
            overridePendingTransition(0, 0);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            finish();

            overridePendingTransition(0, 0);
            startActivity(intent);
        }

    };

}

我非常接近,(至少我认为我是):P

我的手机可以正常工作,只是重置还没有工作:o

【问题讨论】:

  • 究竟是什么不工作?你想用这个按钮做什么?我想念你的 @Override 注释 insde 你的 onClickListeners...
  • 好吧,我正在寻找一个有效的重置按钮。它有一个有效的开始和暂停按钮,但我希​​望我的重置按钮实际上将计数器带回 00:00:00
  • @Wince Ty 您的回复,但它只会给我错误:/
  • 我真的需要一个好的简单的答案,我已经尝试了一千件事,但我只得到错误。

标签: android timer reset


【解决方案1】:

我想你可以用它来重置计时器。

TimerValue.cancel();

        TextView digital_display = (TextView) findViewById(R.id.digital_display);
        digital_display.setText("00:00.0");

【讨论】:

  • 你看我的代码。我把它放在哪里?因为到目前为止的每一次尝试都只会让我出错。求你的帮助:)
  • 抱歉回答迟了。但是您需要提供有关您遇到的错误的信息,以便我们为您提供帮助。这段代码是为了让你可以重置计时器,你把它放在重置按钮中(我确定你试过了)。
  • 来自 ErrorLog 还是来自 LogCat ? (因为 LogCat 很长) - 在模拟器中按“重置”不会给我一个错误,我只是没有得到任何效果,它与“开始”按钮完全一样,它会继续数数。 (ATM ErrorLog 不工作,尝试修复)
  • 虽然当我在模拟器中打开我的应用程序时,Logcat 给了我这个:i57.tinypic.com/azc3n4.png
  • 抱歉,工作网络不允许我看到您的链接 :) 如果您可以复制粘贴,并且 logcat 错误通常会提供很多信息,但通常应该有一个起点例外,将不胜感激。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 2011-11-02
  • 1970-01-01
相关资源
最近更新 更多