我有一个Android活动,我希望计时器在打开时启动,但是当你点击屏幕时我想要显示暂停符号并停止计时器,然后当你再次点击屏幕时,计时器会在它离开了并继续。有点像切换功能。
我有暂停按钮切换正常,计时器开始计时,但我不知道如何在每次点击之间暂停和再次播放。我怎么能做到这一点?
我有一个名为“mVisible”的私有布尔值,因为我试图只在“暂停按钮”不可见或(!mVisible)时播放定时器,但我只得到空指针异常。
谢谢!
JAVA CODE:
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 CODE:
<?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>
答案 0 :(得分:1)
在你的暂停按钮上单击,当你切换ans show play按钮,也停止计时器或删除它以停止计时器,然后当点击播放按钮时,你重新运行一个新的计时器实例但它会更新您的计数器来自之前的值。 初始化时使时间变量类级别