Android 针对整个应用程序的单个警报对话框
我必须在我的应用程序中显示许多错误消息和警告对话框。 我不想使用Toast,我更喜欢使用Android 针对整个应用程序的单个警报对话框,android,android-alertdialog,Android,Android Alertdialog,我必须在我的应用程序中显示许多错误消息和警告对话框。 我不想使用Toast,我更喜欢使用AlertDialog 如何创建和维护一个警报对话框,并仅更改其中的错误消息字符串,而不是在每个活动中创建新的警报对话框 无论我从事什么活动,我都必须能够访问AlertDialog实例来显示和取消它 我怎样才能做到这一点?请给我一些这方面的线索。制作一个类并粘贴此函数…(可能是Utils.java) 并通过写作将其称为 Utils.alertDialogShow(YourActivity.this,"Your
AlertDialog
如何创建和维护一个警报对话框,并仅更改其中的错误消息字符串,而不是在每个活动中创建新的警报对话框
无论我从事什么活动,我都必须能够访问AlertDialog
实例来显示和取消它
我怎样才能做到这一点?请给我一些这方面的线索。制作一个类并粘贴此函数…(可能是Utils.java) 并通过写作将其称为
Utils.alertDialogShow(YourActivity.this,"Your Error Message")
我在助手类中执行类似的操作
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();
}
您可以始终使用alertdialog调用作为方法编写活动基类,然后对于任何活动类而不是使用extends Activity,使用extends MyBaseActivity,然后在需要时通过传递想要输出的字符串来调用该方法。尝试以下操作:
/**
* 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.
将对话框创建为类中带有参数(如消息)的方法,并将其设置为静态,在需要时将其调用为@G_S,并将上下文传递给该方法,您可以在新类中使用该方法。请不要命名实用程序类
Utils
。。。使其具体化、组织化。例如,这可能会出现在AlertUtils
类中,或者出现在DialogUtils
中。想想谁以后可能会维护你的代码,除非它是你自己的个人项目(在这种情况下,只要你记得里面有什么,就可以随意命名)。@ChrisCirefice谢谢你的建议。我会记住下次在stackoverflow以及我当前的编码中的答案。再次感谢。!!这是个坏习惯。改用util类。使用组合而不是继承,因为应用程序中的其他类类型可能需要显示对话框。
vPopup.showValidationDialog("Alert Msg","This is message content", objectOfEditText);//You can pass null as third arg if its not for edittext.