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

rjee0c15  于 2023-01-24  发布在  Android
关注(0)|答案(2)|浏览(144)

由于我是新的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

我正在寻找任何类型的帮助停止时间,并显示在对话框中。
之后,我打算把它保存到数据库从对话框.

zwghvu4y

zwghvu4y1#

您可以调用这个.cancel(),也可以在它的回调方法之一中直接调用cancel(
同时选中How to stop CountDownTimer in android

cunj1qz1

cunj1qz12#

可以使用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();
}

相关问题