如何停止“倒数计时器”并在 Android Studio 的对话框中显示结果?

因为我是 android 开发的新手,所以我需要一些帮助。我正在开发测验应用程序,所以我有分数倒计时。我无法弄清楚如何停止计时器并在自定义对话框中显示。因此,如果每个问题都将完成,则应停止计时器,并且必须在最后一次对话中。在我将它存储到数据库中以按时间和分数对其进行排序之后。


这里是倒计时方法。


private void countD(){

        countDownTimer = new CountDownTimer(timeleftinmilliseconds, 1000) {

            @Override

            public void onTick(long l) {

                timeleftinmilliseconds = l;

                int minutes = (int) (timeleftinmilliseconds/1000)/60;

                int second = (int) (timeleftinmilliseconds/1000)%60;

                String timeletfFormated  = String.format(Locale.getDefault(), "%02d:%02d", minutes, second);

                timetext.setText(timeletfFormated);

            }


            @Override

            public void onFinish() {

                timetext.setText("00:00");

            }

        }.start();

自定义对话框的xml


<TextView

    android:id="@+id/dialog_time_message"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Your Time is: "

    android:layout_below="@id/dialog_message"

    android:textSize="18dp"

    android:layout_marginTop="20dp"

    />

<TextView

    android:id="@+id/dialog_time_score"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_toRightOf="@id/dialog_message"

    android:paddingLeft="10dp"

    android:text="00:00"

    android:textSize="18dp"

    android:layout_marginTop="20dp"

    android:layout_below="@+id/dialog_score"

    />



<EditText

    android:id="@+id/edittext_name"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:hint="Please enter your name"

    android:layout_below="@+id/dialog_time_message"

    android:layout_marginTop="10dp"

    />


拉风的咖菲猫
浏览 274回答 2
2回答

牛魔王的故事

您可以使用 cancel() 方法:private CountDownTimer countDownTimer;public void startCount(){countDownTimer = new CountDownTimer(SECONDS_TO_DISMISS, 1000) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onTick(long time) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;strong text&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; **strong text**&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onFinish() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }.start();}public void stopCount(){&nbsp; &nbsp; countDownTimer.cancel();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java