Android Dialog Box without buttons

前端 未结 9 2522
余生分开走
余生分开走 2021-02-19 06:52

Can i create a dialog box without negative or positive buttons. That destroys it self after specific action?

 AlertDialog.Builder dialog_detect= new AlertDialog.         


        
9条回答
  •  星月不相逢
    2021-02-19 07:23

    Really depends on what "action" is being performed:


     AlertDialog.Builder dialog_detect= new AlertDialog.Builder(MainActivity.this);
     dialog.setTitle("Detecting.....");
     dialog.setMessage("Please Wait");
     dialog.show();
    
     timeConsumingDetectMethod();
    
     dialog.dismiss();
    

    This way you get a frozen UI until timeConsumingDetectMethod() finishes.


    However, the following way runs the action in background, while a very responsive dialog is shown. Also, cancels the action when dialog is cancelled.

    AsyncTask task = new AsyncTask() {
    
            private AlertDialog dialog;
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dialog= new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("Detecting.....");
                dialog.setMessage("Please Wait");
    
                dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {
                        cancel(true);
                    }
                });
    
                dialog.show();
            }
    
            @Override
            protected Void doInBackground(Void... voids) {
                timeConsumingDetectMethod();
                return null;
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                dialog.dismiss();
            }
    
        }.execute();
    

提交回复
热议问题