Android AlertDialog Builder

joe*_*per 5 android builder android-alertdialog

我有一个我显示的警报,但无论我做什么,alertdialog显示一个空白的标题和消息.图标,正面按钮和负面按钮显示正确的描述.以下是我使用的代码片段:在Manifest文件中:

<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="16" />
Run Code Online (Sandbox Code Playgroud)

在我的代码中,我声明:

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
Run Code Online (Sandbox Code Playgroud)

我还声明了上下文:

final Context context = this;
Run Code Online (Sandbox Code Playgroud)

我发出警报:

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


}
Run Code Online (Sandbox Code Playgroud)

然后我从我需要的地方打电话确认,如下:

confirm();
Run Code Online (Sandbox Code Playgroud)

警报显示确定.图标设置setPositiveButton是好的,包含正确的描述setNegativeButton是好的,包含正确的描述

标题为空白消息为空白

有任何想法吗?

Sri*_*Pai 5

你可以使用

        AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this);
        builder.setTitle("Modify Customer Details");
Run Code Online (Sandbox Code Playgroud)

或者

        Dialog dialog = new Dialog(YourActivity.this);
        dialog.setTitle("Payment Options");
Run Code Online (Sandbox Code Playgroud)


Jad*_*eld 3

尝试以这种方式设置消息和标题。这来自对话框的开发人员指南

// 1. 使用其构造函数实例化 AlertDialog.Builder

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Run Code Online (Sandbox Code Playgroud)

// 2. 将各种setter方法链接在一起来设置对话框特性

builder.setMessage(R.string.dialog_message)
       .setTitle(R.string.dialog_title);
Run Code Online (Sandbox Code Playgroud)

// 3. 获取AlertDialog来自create()

AlertDialog dialog = builder.create();
Run Code Online (Sandbox Code Playgroud)