Single alert dialog for entire application

2020-02-26 11:00发布

I have to display many error messages and alert dialogues in my application. I do not want to use Toast, I prefer to use AlertDialog.

Instead of creating a new alert dialog in every activity, how do I create and maintain one alert dialog and just change the error message string in it?

Whatever activity I am in, I must be able to access the AlertDialog instance to show and dismiss it.

How can I achieve this? Kindly give me some lead on this.

4条回答
祖国的老花朵
2楼-- · 2020-02-26 11:26

make one class and paste this function...(may be 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();
        }

and call this by writing..

Utils.alertDialogShow(YourActivity.this,"Your Error Message")
查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-26 11:27

You could always write a base class of Activity with your alertdialog call as a method and then for any of your activity classes instead of using extends Activity, use extends MyBaseActivity and then call the method whenever you need it by passing the string you want output.

查看更多
forever°为你锁心
4楼-- · 2020-02-26 11:37

Try this:

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

    }

}

You can make this class's object anywhere in your application as,

ValidationPopup vPopup = new ValidationPopup(mContext);

And call method showValidationDialog according to your need

vPopup.showValidationDialog("Alert Msg","This is message content", objectOfEditText);//You can pass null as third arg if its not for edittext.
查看更多
唯我独甜
5楼-- · 2020-02-26 11:47

I do something like this in a helper class

 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();
 }
查看更多
登录 后发表回答