如何在多个活动中显示相同的对话框?

时间:2012-12-03 10:08:02

标签: android dialog android-asynctask

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

2 个答案:

答案 0 :(得分:1)

我过去以两种不同的方式做到了。

1)创建一个布局,用作我在需要时显示的对话框(在每个活动布局和隐藏中导入)(您还可以创建一个“空”活动,只显示该对话框以及您是否需要该消息。< / p>

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

//main Activity:
     DialegError da = new DialegError(this);
     da.crearDialeg("APP ERROR", "this is an error");

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

答案 1 :(得分:0)

您可以创建一个显示对话框的按钮,在事件中单击任何显示对话框的代码,然后设置按钮单击定期。

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

希望对你有所帮助