从调用另一个类的警告对话框 [英] Calling an alert dialog from another class
问题描述
我想从另一个调用类的警告对话框,但是这不是让我将其设置为静态。这表明,因为只有最终被允许,这意味着它不能从其他类调用它。我不知道如果我做正确,或者如果它甚至有可能。我警告对话框类2:
I am trying to call an alert dialog from another class but this is not letting me set it to static. It shows as only final is permitted and that means it cannot call it from the other class. I'm not sure if I am doing it correctly or if it is even possible. I have the alert dialog in class 2:
static final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertDialog = new AlertDialog.Builder(this).create();
alertbox.setTitle("Hello");
alertbox.setMessage("Press Continue or Cancel");
alertbox.setPositiveButton("CONTINUE",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertbox.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.setIcon(R.drawable.icon);
这则被调用Class1中:
This then gets called in class1:
QuizValidation.alertbox.show();
其中还无法得到解决。
Which also cannot be resolved.
我也许可以那种,如果我能在2类alertbox设置为静态。
I can probably sort that out if I could set the alertbox in class 2 to static.
应该AP preciate任何意见。
Would appreciate any advice.
感谢
推荐答案
它是一个更好的主意来定义一个基类所有的对话,让我们把它叫做......嗯BaseActivity
Its a better idea to define all your dialogs in a base class , lets call it ... well BaseActivity
Class BaseActivity extends Activity{
int DIALOG_X = 1;
int DIALOG_Y = 2;
int DIALOG_Z = 3;
// More Dialog identifiers
ProgressDialog progressDialog;
AlertDialog alertDialog;
//More dialog objects if you need
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DIALOG_X:
// do the work to define the X Dialog
break;
case DIALOG_Y:
// do the work to define the Y Dialog
break;
default:
dialog = null;
}
return dialog;
}
}
然后在另一个类扩展BaseActivity并调用
Then in another class extend BaseActivity and call
showDialog(DIALOG_X);
当你需要证明Dialog_X
when you need to show Dialog_X
这篇关于从调用另一个类的警告对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!