6

我必须在我的应用程序中显示许多错误消息和警报对话框。我不想使用 Toast,我更喜欢使用AlertDialog.

我如何创建和维护一个警报对话框并只更改其中的错误消息字符串,而不是在每个活动中创建一个新的警报对话框?

无论我从事什么活动,我都必须能够访问AlertDialog实例以显示和关闭它。

我怎样才能做到这一点?请给我一些线索。

4

4 回答 4

20

制作一个类并粘贴此功能...(可能是Utils.java)

public static void alertDialogShow(Context context, String message)
        {
            final AlertDialog alertDialog = new AlertDialog.Builder(context).create();
            alertDialog.setMessage(message);
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int which) 
                {
                    alertDialog.dismiss();
              } 
            }); 
            alertDialog.show();
        }

并通过写作来称呼它..

Utils.alertDialogShow(YourActivity.this,"Your Error Message")
于 2012-11-26T09:32:16.350 回答
4

您始终可以使用您的 alertdialog 调用作为方法编写 Activity 的基类,然后对于您的任何 Activity 类而不是使用扩展 Activity,使用扩展 MyBaseActivity,然后在需要时通过传递您想要输出的字符串来调用该方法。

于 2012-11-26T09:34:48.870 回答
0

尝试这个:

/**
 * Class for showing validation message
 */

package com.prj.utility;

import com.prj.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.prj.utility.System;

public class ValidationPopup {

    Context mContext = null;

    public ValidationPopup(Context mContext) {
        // TODO Auto-generated constructor stub
        this.mContext = mContext;
    }

    /**
     * 
     * @param strTitle - title of dialog
     * @param strMessage - message to be shown in dialog
     * @param value - edit text object
     */
    public void showValidationDialog(String strTitle, String strMessage,
            final EditText value) {
        final AlertDialog dlgValidationPopUp = new AlertDialog.Builder(mContext)
                .create();

        final LayoutInflater lyInflaterForDlgTitle = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout lLayoutCustTitle = (LinearLayout) lyInflaterForDlgTitle
                .inflate(R.layout.custom_title, null);

        TextView tvTitle = (TextView) lLayoutCustTitle
                .findViewById(R.id.tvTitle);
        tvTitle.setText(strTitle);
        dlgValidationPopUp.setCustomTitle(lLayoutCustTitle);
        if (strMessage == null || strMessage.trim().equalsIgnoreCase("")) {

            //If there isn't any message in database for validation of the field, then DEFAULT_MESSAGE will be used
            strMessage = Config.DEFAULT_MESSAGE;
        }

        dlgValidationPopUp.setMessage(strMessage);
        dlgValidationPopUp.setCancelable(true);
        dlgValidationPopUp.setButton("OK",
                new DialogInterface.OnClickListener() {

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

                        //Edittext will be given focus if the field is edit text and it is not null
                        if (value != null)
                            value.requestFocus();
                    }
                });
        dlgValidationPopUp.show();

    }

}

您可以在应用程序中的任何位置创建此类的对象,

ValidationPopup vPopup = new ValidationPopup(mContext);

showValidationDialog并根据您的需要调用方法

vPopup.showValidationDialog("Alert Msg","This is message content", objectOfEditText);//You can pass null as third arg if its not for edittext.
于 2012-11-26T09:35:41.043 回答
0

我在帮助类中做这样的事情

 public static void AlertBox(final Activity activity, final String title, final String message)
 {
     AlertDialog.Builder alertbox = new AlertDialog.Builder(activity);
     alertbox.setTitle(title);
     alertbox.setCancelable(false);
     alertbox.setMessage(message);
     alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int which) {
         activity.finish();
         }
 });

     alertbox.show();
 }
于 2012-11-26T09:32:30.403 回答