How to open a dialog when I click a button?

前端 未结 4 753
星月不相逢
星月不相逢 2020-12-29 23:37

I have a button and I would like to open a dialog when pressed. This is my code:

Button more = (Button) findViewById(R.id.more);
more.setOnClickListener(new          


        
4条回答
  •  情深已故
    2020-12-30 00:20

                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
                builder.setMessage("this is message");
                builder.setTitle("this is title");
    
                //Setting message manually and performing action on button click
                builder.setMessage("Do you want to close this application ?");T
                //This will not allow to close dialogbox until user selects an option
                builder.setCancelable(false);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                Toast.makeText(this, "positive button", Toast.LENGTH_SHORT).show();
                                //builder.finish();
                            }
                        });
                 builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                //  Action for 'NO' Button
                                Toast.makeText(this, "negative button", Toast.LENGTH_SHORT).show();
                                dialog.cancel();
                            }
                        });
    
                //Creating dialog box
                AlertDialog alert = builder.create();
                //Setting the title manually
                //alert.setTitle("AlertDialogExample");
                alert.show();
    

提交回复
热议问题