首页 > 解决方案 > 如何停止“倒数计时器”并在 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"
    />

这也是为了弹出对话框

private void gameOver(){
        final AlertDialog.Builder builder = new AlertDialog.Builder(StartQuiz.this);
        View view = getLayoutInflater().inflate(R.layout.layout_dialog, null);
        //Declare and casting for score and nameField in customized dialog
        final EditText editTextName = (EditText)view.findViewById(R.id.edittext_name);
        final TextView dialog_score_show = (TextView)view.findViewById(R.id.dialog_score);
        //Declare and casting for time in customized dialog
        final TextView time_view_in_cDialog = (TextView)view.findViewById(R.id.dialog_time_score);
        dialog_score_show.setText(""+Score);
        //dialog_time_show.setText(""+countDownTimer);//check this(it should show the final time stopped which also has to be shown in resulst)
        builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //TODO
            }
        });
        builder.setView(view);
        builder.setCancelable(false);
        final AlertDialog dialog = builder.create();
        dialog.show();
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
                new View.OnClickListener() {//clicked submit button
                    @Override
                    public void onClick(View view) {
                        if(!editTextName.getText().toString().isEmpty()) {//checking the empty field(not empty)
                            boolean isInserted = myDb.insertData(editTextName.getText().toString(),
                                        dialog_score_show.getText().toString());//inserting data to database from field
                            //while inserting data it should sort by score and time and delete the last 11th
                            if(isInserted == true){
                                Toast.makeText(StartQuiz.this, "Quiz Completed", Toast.LENGTH_SHORT).show();
                                getData();/*gets all the data name and the result*/ }
                            else
                                Toast.makeText(StartQuiz.this, "Quiz Incompleted", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                        } else//(empty field)
                            Toast.makeText(StartQuiz.this, "Please enter your name", Toast.LENGTH_SHORT).show();
                    }
                }//end of submit button
        );
    }//end of game over

我正在寻找任何帮助来停止时间并显示在对话框中。

之后,我将从对话框中将其保存到数据库中。

标签: javaandroidandroid-alertdialogcountdowncountdowntimer

解决方案


您可以使用 cancel() 方法:

private CountDownTimer countDownTimer;

public void startCount(){

countDownTimer = new CountDownTimer(SECONDS_TO_DISMISS, 1000) {

            @Override
            public void onTick(long time) {
               strong text
            **strong text**
            }

            @Override
            public void onFinish() {

            }

        }.start();
}

public void stopCount(){
    countDownTimer.cancel();
}

推荐阅读