Android close dialog after 5 seconds?

后端 未结 8 801
北海茫月
北海茫月 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:59
    AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
    

    then call dismiss meth it work

    alertDialog .dismiss(); 
    
    0 讨论(0)
  • 2020-12-05 03:05

    This is the code, refer this link:

    public class MainActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // get button
            Button btnShow = (Button)findViewById(R.id.showdialog);
            btnShow.setOnClickListener(new View.OnClickListener() {
                //on click listener
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                    builder.setTitle("How to close alertdialog programmatically");
                    builder.setMessage("5 second dialog will close automatically");
                    builder.setCancelable(true);
    
                    final AlertDialog closedialog= builder.create();
    
                    closedialog.show();
    
                    final Timer timer2 = new Timer();
                    timer2.schedule(new TimerTask() {
                        public void run() {
                            closedialog.dismiss(); 
                            timer2.cancel(); //this will cancel the timer of the system
                        }
                    }, 5000); // the timer will count 5 seconds....
    
                }
            });
        }
    }
    

    HAPPY CODING!

    0 讨论(0)
提交回复
热议问题