Android 如何在多个活动中显示同一对话框?

Android 如何在多个活动中显示同一对话框?,android,dialog,android-asynctask,Android,Dialog,Android Asynctask,我在活动中创建了一个对话框。使用异步任务,我将定期显示该对话框。当我移动到另一个活动时,是否也可以显示对话框?您可以制作一个显示对话框的按钮,如果单击任何显示对话框的代码,并设置按钮“定期单击” public void getRunningClick(){ new Handler().postDelayed(new Runnable() { public void run() { //your code showing dia

我在活动中创建了一个对话框。使用异步任务,我将定期显示该对话框。当我移动到另一个活动时,是否也可以显示对话框?

您可以制作一个显示对话框的按钮,如果单击任何显示对话框的代码,并设置按钮“定期单击”

public void getRunningClick(){
     new Handler().postDelayed(new Runnable() {
            public void run() {
                  //your code showing dialog
            }
        },(2*1000));
}

希望对您有所帮助

您可以制作一个显示对话框的按钮,一旦单击任何显示对话框的代码,您就可以设置按钮单击周期

public void getRunningClick(){
     new Handler().postDelayed(new Runnable() {
            public void run() {
                  //your code showing dialog
            }
        },(2*1000));
}

希望对你是有帮助的

我过去用两种不同的方式做过

1) 创建一个用作对话框的布局(在每个活动布局中导入并隐藏),我会在需要时显示该对话框(您还可以创建一个“空”活动,仅弹出该对话框,如果您需要该消息)

2) 创建一个CustomDialog类并使用它(我用它来处理自定义字体,但我在这段代码中只放一次)



过去我用了两种不同的方法

1) 创建一个用作对话框的布局(在每个活动布局中导入并隐藏),我会在需要时显示该对话框(您还可以创建一个“空”活动,仅弹出该对话框,如果您需要该消息)

2) 创建一个CustomDialog类并使用它(我用它来处理自定义字体,但我在这段代码中只放一次)



假设代码在Avtivity A中,当我移动到activity B时,对话框会打开吗?你在独立类中创建此方法,每个类,例如类A和类B调用此方法。不,我不知道活动用户在哪个活动中,我需要找到活动类并显示它。假设代码在Avtivity A中,当我移动到活动B时,对话框会打开吗?你在独立类中创建此方法,每个类(例如A类和B类)都调用此方法。不,我不知道活动用户在哪个活动中,我需要找到活动类并显示它。请检查我的答案。。。。在这里检查我的答案。。。。
//Error class
public class DialegError {
    private Activity a = null;

    public DialegError(Activity activity){
            a=activity;
    }

    /**
     * Default NO-MESSAGE errorDialog
     */
    public void crearDialeg(String titol){
        AlertDialog dialog = new AlertDialog.Builder(a)
            .setTitle( titol )
//          .setIcon(R.drawable.)
            .setPositiveButton( a.getString(R.string.button_aceptar), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            })
            .show();
    }

    /**
     * Default errorDialog
     */
    public void crearDialeg(String titol, String cos){
        AlertDialog dialog = new AlertDialog.Builder(a)
            .setTitle( titol )
            .setMessage( cos )
//          .setIcon(R.drawable.)
            .setPositiveButton( a.getString(R.string.button_aceptar), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    return;
                }
            })
            .show();

     //Personalized font. No way to deal with the title text.
     Typeface font=Typeface.createFromAsset(a.getAssets(),"fonts/font_name.ttf");
     TextView textView = (TextView)dialog.findViewById(android.R.id.message);
     textView.setTypeface(font);
     textView =  (TextView)dialog.findViewById(android.R.id.button1);
     textView.setTypeface(font);
    }

    /**
     * Error Dialog that closes the invoker activity.
     */
    public void crearDialegError(String titol, String cos, int err){
        final Activity activitat = a;
        final int error = err;
        AlertDialog dialog = new AlertDialog.Builder(activitat)
            .setTitle( titol )
            .setMessage( cos )
//          .setIcon(R.drawable.)
            .setPositiveButton( activitat.getString(R.string.button_aceptar), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    activitat.setResult(error, new Intent());
                    activitat.finish();
                }
            })
            .show();
    }
}