【问题标题】:Start timer on a button click and stop it with 10 button clicks单击按钮启动计时器并单击 10 次按钮停止计时器
【发布时间】:2014-05-30 02:03:54
【问题描述】:

到目前为止,我的按钮点击计数器正在工作,它在点击 10 次时停止。我唯一的问题是计时器。每次我按下按钮它都会重置计时器。我希望按钮在第一次单击时启动计时器并在第 10 次单击时停止计时器,但我很难过,不知道如何准确编码。有谁知道我怎样才能让它工作?

Java 代码:

package com.example.thirtytapgametest;

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


public class MainActivity extends Activity {

private int mCount = 0;

private ImageButton startbutton;

private TextView timerValue;

private long startTime = 0L;

private Handler customHandler = new Handler();

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

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

    final TextView countTextView = (TextView) findViewById(R.id.TextViewCount);

    timerValue = (TextView) findViewById(R.id.timerValue);      
    startbutton = (ImageButton) findViewById(R.id.imageButton1);
    startbutton.setOnClickListener(new View.OnClickListener() {


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

            mCount++;
            countTextView.setText("Taps: " + mCount);
            if(mCount == 10) {view.setEnabled(false);}
        }           
    });

}



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);
    }
    };

}

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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:background="@drawable/thirty_tap_game_background">

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="138dp"
    android:background="@drawable/test_play_button" />

<TextView
    android:id="@+id/timerValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/imageButton1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="32dp"
    android:textSize="50sp"
    android:textColor="#FF0000"
    android:text="@string/timerVal" />

<TextView
    android:id="@+id/TextViewCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="73dp"
    android:text="@string/countVal" 
    android:textSize="30sp"
    android:textColor="#FF0000"/>

</RelativeLayout>

【问题讨论】:

    标签: android button timer counter


    【解决方案1】:

    以下是您解决问题的代码。作为'timeSwapBuff。未使用,我已经注释了那行代码。

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final TextView countTextView = (TextView) findViewById(R.id.TextViewCount);
    
            timerValue = (TextView) findViewById(R.id.timerValue);
            startbutton = (ImageButton) findViewById(R.id.imageButton1);
            startbutton.setOnClickListener(new View.OnClickListener() {
    
                public void onClick(View view) { 
                    if (mCount ==0) {
                    startTime = SystemClock.uptimeMillis();
                    customHandler.postDelayed(updateTimerThread, 0);
                    }
    
                        mCount++;
                        countTextView.setText("Taps: " + mCount);
                        if(mCount == 10) {
                            view.setEnabled(false);
    
                            customHandler.removeCallbacks(updateTimerThread);
                            }
                }           
            });
    
        }
    
        private Runnable updateTimerThread = new Runnable() {
    
    
            public void run() {
                updatedTime = 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);
            }
    
            };
        }
    

    【讨论】:

    • @user3689022 - 请为我的答案投票,点击“向上”按钮,该答案对您有用
    • 我肯定会通过单击向上按钮来投票赞成您的答案,但现在我的声誉还不够高,无法这样做。另外,我还有一个问题希望您能帮我解答。使用我当前的代码,我一直在尝试设置一个警报对话框,以在达到第 10 次点击时弹出,但它无法正常工作并在我的代码中不断显示错误。知道如何在我的代码中设置一个带有按钮的警报对话框以再次玩游戏吗?谢谢!
    • 谢谢。请在您的代码和 logcat 中发布另一个问题,以便我可以肯定地帮助您...如果我不是在最坏的情况下,整个 SO 都会以这种方式为您提供支持!
    猜你喜欢
    • 1970-01-01
    • 2021-03-26
    • 2013-02-23
    • 2021-11-23
    • 1970-01-01
    • 2019-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    相关资源
    最近更新 更多