简体   繁体   中英

How to stop the “Countdown timer ” and show the result in dialog in Android studio?

as I am new on android development I need some help. I am developing quiz application, so I have score countdown timer. I cannot figure it out how to stop the timer and to show of in customized dialog. So if every question will be completed it should stop the timer and it has to be in dialog final time. after I will store it into database to sort it the rank by timing and score.

Here is countdown method.

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();

The xml for customized dialog

<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"
    />

And also this is for dialog to pop up

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

I`m looking for any kind of help to Stop the time and to be shown in dialog.

After, I am going to save it into database from dialog.

You can call this.cancel() or simply cancel() inside one of its callback methods

and also you check How to stop CountDownTimer in android

You can use the cancel() method:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM