How to create modal dialog box in android

后端 未结 4 1185
野的像风
野的像风 2021-02-01 19:53

i want create modal dialog box for my application.

so when modal dialog box open the other activities are blocked. no event are done like back button press or home butto

4条回答
  •  孤城傲影
    2021-02-01 20:31

    There are many kind of Dialogs in Android. Please take a look at Dialogs. I guess what you are looking for is something like AlertDialog . This is the example of how you can implement on BackPress button.

    @Override
    public void onBackPressed() {
        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Do you want to logout?");
        // alert.setMessage("Message");
    
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                //Your action here
            }
        });
    
        alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });
    
        alert.show();
    
    }
    

提交回复
热议问题