Android Dialog Box without buttons

2020-08-10 19:45发布

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

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

标签: android
9条回答
地球回转人心会变
2楼-- · 2020-08-10 19:49

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<Void,Void,Void> task = new AsyncTask<Void, Void, Void>() {

        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();
查看更多
三岁会撩人
3楼-- · 2020-08-10 19:54

You can try Custom Dialog design u r on Dialog and use it as u wish to use them

final Dialog dialog= new Dialog(context);
dialog.setContentView(R.layout.pre_confirmation_dailog); 
dialog.setTitle("Details...");
dialog.show();
查看更多
爷的心禁止访问
4楼-- · 2020-08-10 19:57

You can call alertDialog .dismiss () after any action.

查看更多
劫难
5楼-- · 2020-08-10 19:57

Implement a timer and set it to dismiss the DialogBox after a few seconds.

查看更多
对你真心纯属浪费
6楼-- · 2020-08-10 20:03

You can do this very easily.

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

// set title
alertDialogBuilder.setTitle("Your Title");

// set dialog message
alertDialogBuilder.setMessage("Message here!").setCancelable(false);

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();

// show it
alertDialog.show();

// After some action
alertDialog.dismiss();

If you have a reference to the AlertDialog somewhere else, you can still call alertDialog.dismiss(). This closes the dialog.

查看更多
一夜七次
7楼-- · 2020-08-10 20:10

You can also write crocboy's code like this:

AlertDialog alertDialog = new AlertDialog.Builder(context)    
    .setTitle("Your Title")
    .setMessage("Message here!")
    .setCancelable(false)
    .create();

alertDialog.show();

// After some action
alertDialog.dismiss(); 

It does exactly the same thing, it's just more compact.

查看更多
登录 后发表回答