Android close dialog after 5 seconds?

后端 未结 8 830
北海茫月
北海茫月 2020-12-05 02:00

I\'m working on an accesibility app. When the user wants to leave the app I show a dialog where he has to confirm he wants to leave, if he doesn\'t confirm after 5 seconds t

8条回答
  •  时光取名叫无心
    2020-12-05 02:52

    I added automatic dismiss with the time remaining shown in the positive button text to an AlertDialog.

    AlertDialog dialog = new AlertDialog.Builder(getContext())
            .setTitle(R.string.display_locked_title)
            .setMessage(R.string.display_locked_message)
            .setPositiveButton(R.string.button_dismiss, null)
            .create();
    
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            final Button positiveButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            final CharSequence positiveButtonText = positiveButton.getText();
            new CountDownTimer(AUTO_DISMISS_MILLIS, 100) {
                @Override
                public void onTick(long millisUntilFinished) {
                    positiveButton.setText(String.format(Locale.getDefault(), "%s (%d)",
                            positiveButtonText,
                            TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) + 1));
                }
    
                @Override
                public void onFinish() {
                    dismiss();
                }
            }.start();
    
        }
    });
    

提交回复
热议问题