gpt4 book ai didi

android - 如何在多个 Activity 中显示相同的对话框?

转载 作者:行者123 更新时间:2023-11-30 03:53:42 24 4
gpt4 key购买 nike

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

最佳答案

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

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

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

关于android - 如何在多个 Activity 中显示相同的对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13680992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com