6

我有一个显示的警报对话框,但无论我做什么,警报对话框都会显示一个空白的标题和消息。图标、正按钮和负按钮显示正确,说明正确。这是我使用的代码片段: 在清单文件中:

<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="16" />

在我的代码中,我声明:

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

我还声明了上下文:

final Context context = this;

我将警报放在:

public void confirm() {

    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

        // set title

        alertDialogBuilder.setTitle("This is title");
        alertDialogBuilder.setIcon(R.drawable.ic_delete);

        // set dialog message
        alertDialogBuilder
        .setMessage("This is the message")
            .setCancelable(false)
            .setPositiveButton(R.string.yes,new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    MainActivity.this.finish();
                }
              })
            .setNegativeButton(R.string.no,new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();



    // show it
            alertDialog.show();


}

然后我从我需要的地方调用确认,如下所示:

confirm();

警报显示正常。图标已设置 setPositiveButton 很好,包含正确的描述 setNegativeButton 很好,包含正确的描述

标题为空 消息为空

有任何想法吗?

4

4 回答 4

5

你可以使用

        AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
        builder.setTitle("Modify Customer Details");

或者

        Dialog dialog = new Dialog(YourActivity.this);
        dialog.setTitle("Payment Options");
于 2013-10-29T02:15:59.233 回答
2

Try setting the message and title this way. This from the developer's guide on Dialogs

// 1. Instantiate an AlertDialog.Builder with its constructor

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics

builder.setMessage(R.string.dialog_message)
       .setTitle(R.string.dialog_title);

// 3. Get the AlertDialog from create()

AlertDialog dialog = builder.create();
于 2012-12-03T01:21:35.527 回答
0

它与您在上下文(“this”)中的主题相关。您需要检查此上下文。或者你应该膨胀你自己的视图,然后调用 setView 把它放进去。

于 2012-12-03T01:28:54.347 回答
0

请将alertDialog创建行更改为:

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, android.R.style.Theme_Material_Light_Dialog_Alert);
于 2018-12-02T08:03:48.453 回答