6

アプリケーションで多くのエラーメッセージとアラートダイアログを表示する必要があります。Toastは使いたくないので、使いたいですAlertDialog

すべてのアクティビティで新しいアラートダイアログを作成する代わりに、1つのアラートダイアログを作成して維持し、その中のエラーメッセージ文字列を変更するにはどうすればよいですか?

AlertDialogどのようなアクティビティを行っていても、インスタンスにアクセスして表示および非表示にできる必要があります。

どうすればこれを達成できますか?親切にこれについて私にいくつかのリードを与えてください。

4

4 に答える 4

20

1つのクラスを作成し、この関数を貼り付けます...(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の基本クラスを記述し、extends Activityを使用する代わりに、任意のアクティビティクラスに対して、extends MyBaseActivityを使用し、出力する文字列を渡すことで、必要なときにいつでもメソッドを呼び出すことができます。

于 2012-11-26T09:34:48.870 に答える
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 に答える
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 に答える