33

这是我的警报代码,按下按钮时会显示一条消息。如何使 OK 按钮关闭?

    Button bm1 = (Button) findViewById(R.id.button1);
    bm1.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setCancelMessage(null);
            alert.show();
        }
    });
}}      

我已经放alert.setCancelMessage(null)了,但它没有显示任何关闭按钮。

解决方案:

      Button bm1 = (Button) findViewById(R.id.button1);
       bm1.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Builder alert = new AlertDialog.Builder(screen4.this);
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setPositiveButton("OK", null);
            alert.show();
        }
    });
4

9 回答 9

48

将您的警报对话框更改为:

Builder alert = new AlertDialog.Builder(screen4.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK",null);
alert.show();    
于 2013-01-18T11:56:24.787 回答
12

您可以使用它为对话框创建确定和取消按钮,

AlertDialog.Builder builder = new AlertDialog.Builder(
                        getApplicationContext());
                builder.setCancelable(true);
                builder.setTitle("Title");
                builder.setInverseBackgroundForced(true);
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
于 2013-01-18T11:56:33.367 回答
4

难道不能只使用一个积极的按钮来完成吗?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
       .setCancelable(false)
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });
AlertDialog alert = builder.create();
alert.show();
于 2015-02-21T10:25:00.280 回答
1
    bt = (Button) findViewById(R.id.button1);
    bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ab = new AlertDialog.Builder(MainActivity.this);

            ab.setTitle("confirm");
            ab.setIcon(R.drawable.ic_launcher);
            ab.setMessage("Do u want to close this application?");

            ab.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int i) {
                    Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
                }
            });

            ab.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int i) {
                    Toast.makeText(MainActivity.this, "cancle", Toast.LENGTH_SHORT).show();
                }
            });

            ab.show();
        }
    });
于 2015-03-03T09:43:28.390 回答
1

您可以使用setPositiveButtonsetNegativeButton

    alert.setPositiveButton("OK", new OnClickListener() {

        @Override
        public void onClick(DialogInterface alert, int which) {
            // TODO Auto-generated method stub
            //Do something
            alert.dismiss();
        }
    });
    alert.setNegativeButton("Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface alert, int which) {
            // TODO Auto-generated method stub
            alert.dismiss();
        }
    });

编辑:

alert.setButton("OK", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        // Do something
        alert.dismiss();
        }
    });
alert.setButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

        alert.dismiss();
        }
    });
于 2013-01-18T11:55:16.470 回答
0

检查 API 中的 AlertDialog.Builder 页面:http: //developer.android.com/reference/android/app/AlertDialog.Builder.html

您可以分别使用 setPositiveButton、setNeutralButton 和 setNegativeButton 方法设置正、中性和负按钮

于 2013-01-18T11:58:45.887 回答
0

alert.setPositiveButton 或 alert.setNegativeButton

于 2013-01-18T11:55:14.643 回答
0
Button bm1 = (Button) findViewById(R.id.button1);
    bm1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setCancelMessage(null);

            alert.setButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    //do somthing or dismiss dialog by                dialog.dismiss();

                }
            });

            alert.show();
        }
    });
}}      
于 2013-01-18T11:57:00.043 回答
0
    Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    final AlertDialog dialog = new AlertDialog.Builder(screen4.this).create();
    dialog.setTitle("Doctor");
    dialog.setMessage("message");
    dialog.setButton("Ok",new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
            // here you can add functions

           }
    });
    dialog.setButton2("Cancel",new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
        // here you can add functions
               dialog.dismiss();
    }
    });
    dialog.show();
    }};      
于 2013-01-18T12:02:05.853 回答