- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在使用 Android CountDownTimer 时遇到问题。
我正在尝试学习一些 JAVA/Android,并开始开发一个简单的应用程序,供我自己在煮咖啡时使用。我无法让倒计时器的 .cancel ()
函数正常工作。我已阅读此论坛和其他论坛上的多个主题。
我的Java代码:
package jandidrik.kaffe;
import android.content.Context;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.view.View;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity implements OnSeekBarChangeListener {
private SeekBar WaterSeekBar, CoffeeSeekBar, BloomingSeekBar, BrewingSeekBar;
private TextView WaterText, CoffeeText, BloomingText, BrewingText, TimerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WaterSeekBar = (SeekBar) findViewById(R.id.WaterSeekBar);
CoffeeSeekBar = (SeekBar) findViewById(R.id.CoffeeSeekBar);
BloomingSeekBar = (SeekBar) findViewById(R.id.BloomingSeekBar);
BrewingSeekBar = (SeekBar) findViewById(R.id.BrewingSeekBar);
WaterText = (TextView) findViewById(R.id.WaterText);
CoffeeText = (TextView) findViewById(R.id.CoffeeText);
BloomingText = (TextView) findViewById(R.id.BloomingText);
BrewingText = (TextView) findViewById(R.id.BrewingText);
TimerText = (TextView) findViewById(R.id.Timer);
WaterSeekBar.setOnSeekBarChangeListener(this);
CoffeeSeekBar.setOnSeekBarChangeListener(this);
BloomingSeekBar.setOnSeekBarChangeListener(this);
BrewingSeekBar.setOnSeekBarChangeListener(this);
WaterSeekBar.setProgress(1000);
WaterText.setText(Integer.toString(WaterSeekBar.getProgress()));
CoffeeSeekBar.setProgress(63);
CoffeeText.setText(Integer.toString(CoffeeSeekBar.getProgress()));
Long Minutes = TimeUnit.SECONDS.toMinutes(BrewingSeekBar.getProgress());
Long Seconds = BrewingSeekBar.getProgress() - TimeUnit.MINUTES.toSeconds(Minutes);
BrewingText.setText("" + String.format("%01d:%02d", Minutes, Seconds));
}
@Override
public void onProgressChanged(SeekBar Bar, int progress, boolean fromUser) {
switch (Bar.getId()) {
case R.id.WaterSeekBar:
WaterText.setText(Integer.toString(progress));
break;
case R.id.CoffeeSeekBar:
CoffeeText.setText(Integer.toString(progress));
break;
case R.id.BloomingSeekBar:
BloomingText.setText(Integer.toString(progress));
break;
case R.id.BrewingSeekBar:
Long Minutes = TimeUnit.SECONDS.toMinutes(BrewingSeekBar.getProgress());
Long Seconds = BrewingSeekBar.getProgress() - TimeUnit.MINUTES.toSeconds(Minutes);
BrewingText.setText("" + String.format("%01d:%02d", Minutes, Seconds));
break;
}
}
@Override
public void onStartTrackingTouch(SeekBar Bar) {
}
@Override
public void onStopTrackingTouch(SeekBar Bar) {
}
public void Brew(View V) {
final Button BrewButton = (Button) V;
final String ButtonText = BrewButton.getText().toString();
CountDownTimer Count = new CountDownTimer(BrewingSeekBar.getProgress() * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Long Minutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
Long Seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(Minutes);
TimerText.setText("" + String.format("%01d:%02d", Minutes, Seconds));
int VibrateTime = BrewingSeekBar.getProgress() - BloomingSeekBar.getProgress();
if ((TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)) == VibrateTime) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate 500ms
v.vibrate(500);
Toast.makeText(MainActivity.this, "Blooming is finnished!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFinish() {
TimerText.setText("Finished!");
}
};
if (ButtonText.equals("Start Brewing!")) {
Count.start();
BrewButton.setText("Stop Timer!");
} else {
Count.cancel();
BrewButton.setText("Start Brewing!");
}
}
}
我的 XML 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:keepScreenOn="true">
tools:context="jandidrik.kaffe.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adjust Amount of water (ml)"
android:layout_marginBottom="8dp"
android:layout_marginTop="16dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<SeekBar
android:id="@+id/WaterSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="1200"
android:progress="1000"
android:layout_weight="2"
/>
<TextView
android:id="@+id/WaterText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="14"
android:text="1000"
android:layout_marginLeft="8dp"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Adjust Amount of coffee (g)"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<SeekBar
android:id="@+id/CoffeeSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="63"
android:layout_weight="2"
/>
<TextView
android:id="@+id/CoffeeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="63"
android:layout_weight="14"
android:layout_marginLeft="8dp"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:layout_marginTop="52dp"
android:layout_marginBottom="52dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set the blooming time (s)"
android:layout_marginBottom="8dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<SeekBar
android:id="@+id/BloomingSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="60"
android:progress="30"
android:layout_weight="2"
/>
<TextView
android:id="@+id/BloomingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="30"
android:layout_weight="14"
android:layout_marginLeft="8dp"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set the total brewing time (m:ss("
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<SeekBar
android:id="@+id/BrewingSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="480"
android:progress="240"
android:layout_weight="2"
/>
<TextView
android:id="@+id/BrewingText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4 min 0 s"
android:layout_weight="14"
android:layout_marginLeft="8dp"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/BrewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Brewing!"
android:layout_centerInParent="true"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:onClick="Brew"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Chronometer
android:id="@+id/Timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:format="%s:%s"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</LinearLayout>
最佳答案
您正在尝试停止 CountDownTimer
的错误实例。修复代码的最简单方法就是将 Count
移出方法。
private CountDownTimer currentCount;
public void Brew(View v) {
final Button BrewButton = (Button) V;
final String ButtonText = BrewButton.getText().toString();
if (ButtonText.equals("Stop Timer!") && currentCount != null) {
currentCount.cancel();
currentCount = null;
BrewButton.setText("Start Brewing!");
return;
}
currentCount = new CountDownTimer(BrewingSeekBar.getProgress() * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
Long Minutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
Long Seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(Minutes);
TimerText.setText("" + String.format("%01d:%02d", Minutes, Seconds));
int VibrateTime = BrewingSeekBar.getProgress() - BloomingSeekBar.getProgress();
if ((TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)) == VibrateTime) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate 500ms
v.vibrate(500);
Toast.makeText(MainActivity.this, "Blooming is finnished!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFinish() {
TimerText.setText("Finished!");
}
};
currentCount.start();
BrewButton.setText("Stop Timer!");
}
关于java - Android CountDownTimer 中的 .cancel() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42101820/
在我的 Android 应用程序中,我运行一个计时器并在其他事件发生时取消它: class MyTimerTask extends TimerTask { override bool
如果有待处理的警报,PendingIntent.cancel() 如何影响 AlarmManager。 我应该对两个对象( Intent 和警报管理器)调用取消来取消警报吗?有人可以解释一下他们是如何
我正在研究如何取消闹钟,我遇到了这两种方法。应该在什么情况下使用哪一个,为什么?它们是一样的吗? 我目前正在这样做: Intent alarmIntent = new Intent(ChangeAla
我与一位同事讨论了在表单中取消的“网络标准”是什么。在我们的讨论中,我们以“更改密码”页面为例。我们设计了“发送”按钮和“取消”按钮。两者设计相同。 他声称在网络标准中,取消按钮不再是一个按钮,而是一
我一直在探索协程,我很惊讶地发现在 Job 上调用的 cancel 实际上并没有取消作业,而 cancel 在 scope 上调用会立即取消从 scope 启动的协程。谁能解释一下? 作业取消示例。
我正在使用 iOS 应用内购买。 我想请求最新的取消日期和取消总数,最好是 100% 正确的结果。 我正在使用 Apple 的 verifyreceipt 调用 ( https://developer
我在 ionic 2 应用程序中使用谷歌地图,我希望我的标记根据 map 的缩放级别显示/消失。所以我在函数“ionViewDidEnter”中添加了以下行: this.map.addList
在我们的网络应用程序中,我将 Angular-Materials $mdDialog 与确认对象一起使用。是否可以将按钮的顺序从取消-确定更改为确定-取消?并将初始焦点设置为取消按钮?也许通过 CSS
在我的表单“别名”的Form_beforeupdate()事件上,我有这个... If IsNull(Me.txtFName) And IsNull(Me.txtLName) Then MsgBox
在 QInputDialog 中,如何去掉 OK 和 Cancel 按钮中的图标? 注意取消和确定的图标。我查看了属性按钮,不知道如何删除它们。 最佳答案 解决方案的策略是先获取按钮,但是这些属于QD
我正在使用 MFMailComposeViewController Controller ,如下所示: MFMailComposeViewController *picker1 = [[MFMailC
当我尝试连接在不同机器上运行的对等点时出现此错误。我在订购者的 docker 日志中发现此错误。在不同机器上运行的 peer2 的 docker 日志中有错误 获取连接失败:无法连接到任何端点:[or
我注意到,当用户通过提示窗口输入文本时,提示将返回null并继续继续执行代码。我希望取消按钮按照其指示执行操作并取消。我尝试了几个 if 语句,包括: var x= prompt("Please en
我有一个定制对话框winform。在它上面,我有一个标签,文本框和2个按钮(“确定”和“取消”)。它还声明并定义重载的执行方法以传递不同的参数列表。 对话框Winform的调用方式如下: var th
我刚刚使用 Visual Studio 2017 安装了 Xamarin 并创建了一个新的空白应用程序。 当我按 F5 运行应用程序时,出现以下错误:构建已被取消。但是在构建解决方案时(ctrl+sh
Closed. This question needs debugging details。它当前不接受答案。 想改善这个问题吗?更新问题,以便将其作为on-topic用于堆栈溢出。 3年前关闭。 I
我在使用 NgRx 的 Angular 应用程序中使用轮询方案。 为了简化事情,我有如下内容: public stopPolling$ = createEffect(() => this.ac
在我读到的规范中: The bubbles and cancelable attributes must return the values they were initialized to. 我找不
我有一个 dataGetter 类,在其中加载必要的数据(部分 url 地址、电子邮件等),然后调用 AsyncTask。我使用一个抽屉菜单,其中每个 fragment 在创建时都会从新线程调用 da
我在使用 NgRx 的 Angular 应用程序中使用轮询方案。 为了简化事情,我有如下内容: public stopPolling$ = createEffect(() => this.ac
我是一名优秀的程序员,十分优秀!