【问题标题】:Incrementing Timer - don't know how to pause and continue递增计时器 - 不知道如何暂停和继续
【发布时间】:2018-02-25 18:34:13
【问题描述】:

我有一个 Android 活动,我希望计时器在打开时启动,但是当您单击屏幕时,我希望显示暂停符号并停止计时器,然后当您再次单击屏幕时,计时器会在哪里它停止并继续。有点像切换功能。

我的暂停按钮切换正常,计时器开始计数正常,但我不知道如何在每次点击之间暂停和再次播放。我怎样才能做到这一点?

我有一个名为“mVisible”的私有布尔值,因为我试图让计时器仅在“暂停按钮”不可见或 (!mVisible) 时播放,但我只得到一个空指针异常。

谢谢!!

JAVA 代码:

package com.example.jonathan.om11;

import android.media.Image;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import java.util.Timer;
import java.util.TimerTask;

public class Environment1Activity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "Environment1Activity";
    //Initiate for Timer
    protected int count = 0;
    private ImageView playerPause;
    private ImageView backgroundImageBtn;
    private boolean mVisible;

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

        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);

        playerPause = (ImageView) findViewById(R.id.pauseBtn);
        playerPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle();
            }
        });

        backgroundImageBtn = (ImageView) findViewById(R.id.rainforest_img);
        backgroundImageBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggle();
            }
        });
    }

    private void toggle() {
        if (mVisible) {
            hide();

        } else {
            show();
        }
    }

    //Pause
    public void show() {
        playerPause = (ImageView) findViewById(R.id.pauseBtn);
        playerPause.setVisibility(View.VISIBLE);
        mVisible = true;
        Log.d(TAG, "Player: PAUSED ");
    }


    //Play
    public void hide() {
        playerPause = (ImageView) findViewById(R.id.pauseBtn);
        playerPause.setVisibility(View.GONE);
        mVisible = false;
    }

    //Hide pause button upon creating activity
    public void hidePauseBtn() {
        playerPause = (ImageView) findViewById(R.id.pauseBtn);
        playerPause.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {

    }

    public void timer() {
        Timer timer = new Timer();

        //Count environment runtime
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        count++;
                        Log.d(TAG, "run: Time is " + count);
                    }
                });
            }
        }, 1000, 1000);
    }
}

XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.jonathan.om11.Environment1Activity">

        <ImageView
            android:id="@+id/rainforest_img"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/rainforest_env" />

        <ImageView
            android:id="@+id/pauseBtn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintEnd_toStartOf="@+id/rainforest_img"
            app:layout_constraintStart_toStartOf="parent"
            app:srcCompat="@drawable/ic_pause_white_48dp" />

    </android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android timer android-runonuithread


    【解决方案1】:

    在您的暂停按钮上单击,即当您切换和显示播放按钮时,也停止计时器或将其删除以停止计时器,然后当单击播放按钮时,您重新运行计时器的新实例,但它会更新你的计数器从以前的值。 初始化时使时间变量类级别

    【讨论】:

    • 谢谢,我会尝试做出这些改变,但我还是 Android 新手。我已经有了切换();为“playerPause”实例放置的函数,这就是你的意思吗?你能把我复制的代码拿来举个例子吗?
    猜你喜欢
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多