单击按钮启动计时器并单击10次按钮停止计时器

时间:2014-05-30 02:03:54

标签: android button timer counter

到目前为止,我的按钮水龙头计数器工作,它在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>

1 个答案:

答案 0 :(得分:0)

以下是您解决问题的代码。作为'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);
        }

        };
    }