如何创建通知取消对话框?

时间:2011-10-01 10:57:30

标签: android dialog notifications

我有一个通知,告诉用户下载文件及其进度。
我想创建一个对话框,其中一些文本视图显示一些信息,两个按钮是关闭和停止(停止下载) 我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

您可以使用AlertDialog类

    AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
     alertbox.setMessage("Downloading");

    alertbox.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {

        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {
           ..... //cancel code
        }
    });

    alertbox.setNegativeButton("Pause", new DialogInterface.OnClickListener() {

        // do something when the button is clicked
        public void onClick(DialogInterface arg0, int arg1) {
            .... //pause code
        }
    });

    // display box
    alertbox.show();

答案 1 :(得分:0)

您可以使用自定义对话框,其中您的文本视图在xml中定义,您可以使用xml在自定义对话框中设置为视图。

你可以像这样编码: -

@Override
protected Dialog onCreateDialog(int id) {
    LayoutInflater factory = null;
        factory = LayoutInflater.from(this);
        dialogView = factory.inflate(R.layout.yourXml, null);

        return new AlertDialog.Builder(yourActivity.this)
            .setTitle(R.string.rate_title)
            .setView(dialogView)
            .setPositiveButton("Stop", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

              // Add your stop code here
                }
            })
            .setNegativeButton("Close", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                // dismiss dialog here

                }
            })
            .create();
相关问题