How to open a dialog when I click a button?

前端 未结 4 721
星月不相逢
星月不相逢 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:14

    Aman Alam's souliton is good, but the .setButton() part gave me an error. So I implemented it in kotlin and fixed the error.

    Kotlin

        val dialog = AlertDialog.Builder(this)
    
        dialog.setTitle("Title")
              .setMessage("Write your message here.")
              .setPositiveButton("YES") { dialog, whichButton ->
                   // DO YOUR STAFF
              }
             .setNegativeButton("NO") { dialog, whichButton ->
                   // DO YOUR STAFF 
                   // dialog.close()
              }
    
        dialog.show()
    

    The result of the code

    More about dialogs: https://developer.android.com/guide/topics/ui/dialogs

    0 讨论(0)
  • 2020-12-30 00:15

    As @Roflcoptr has said, you haven't called alertDialog.show() method. thus your dialog doesn't appear.

    Here's your edited code:

    Button more = (Button) findViewById(R.id.more);
    more.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            //Intent myIntent = new Intent(view.getContext(), agones.class);
            //startActivityForResult(myIntent, 0);
    
    
            AlertDialog alertDialog = new AlertDialog.Builder(<YourActivityName>this).create(); //Read Update
            alertDialog.setTitle("hi");
            alertDialog.setMessage("this is my app");
    
            alertDialog.setButton("Continue..", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                  // here you can add functions
               }
            });
    
            alertDialog.show();  //<-- See This!
        }
    
    });
    

    if you write this instead of <ActivityName>.this, then it is going to take the reference of View.OnClickListener since this is currently being accessed inside it. You need to give your Activity's name there.

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-12-30 00:24

    Your dialog isn't displayed, because you don't call AlertDialog#show.

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