单击按钮时打开对话框

时间:2011-01-31 11:47:36

标签: android dialog

我有一个按钮,我想在按下时打开一个对话框。这是我的代码:

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(this).create();
        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
        }
        });
    }
});

4 个答案:

答案 0 :(得分:36)

正如@Roflcoptr所说,你没有调用alertDialog.show()方法。因此你的对话框不会出现。

这是您编辑的代码:

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!
    }

});

如果你写this而不是<ActivityName>.this,那么它将引用View.OnClickListener,因为this当前正在其中进行访问。你需要在那里提供你的活动名称。

答案 1 :(得分:11)

您的对话框未显示,因为您没有拨打AlertDialog#show

答案 2 :(得分:0)

Aman Alam's souliton很好,但是.setButton()部分给了我一个错误。所以我在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

有关对话框的更多信息:https://developer.android.com/guide/topics/ui/dialogs

答案 3 :(得分:-2)

            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();