【问题标题】:How to display same dialog in multiple activitiy?如何在多个活动中显示相同的对话框?
【发布时间】:2023-03-23 22:15:01
【问题描述】:

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

【问题讨论】:

标签: android dialog android-asynctask


【解决方案1】:

我过去以两种不同的方式做到这一点。

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

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

【讨论】:

    【解决方案2】:

    您可以制作一个显示对话框的按钮,在事件 onclick 显示对话框的任何代码时,您可以设置按钮单击周期。

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

    希望对你有帮助

    【讨论】:

    • 假设代码在 Avtivity A 中,当我移动到 Acitivity B 时,对话框会打开吗?
    • 你在类中独立创建这个方法,每个类,例如A类和B类都调用这个方法。
    • 不,我不知道用户在哪个活动中,我需要找到活动类并需要显示它。
    猜你喜欢
    • 2011-03-10
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多