您可以在活动外部显示对话框,但您需要对Context对象的引用.
此类不是活动,但可以创建和显示对话框:
public class DialogExample {
public Context mContext;
public DialogExample(Context context) {
mContext = context;
}
public void dialogExample() {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage("Dialog created in a separate class!");
builder.show();
}
Run Code Online (Sandbox Code Playgroud)
然后你可以在Activity中引用它:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
DialogExample otherClass = new DialogExample(this);
otherClass.dialogExample();
}
Run Code Online (Sandbox Code Playgroud)
当您使用实用程序方法创建在应用程序中的多个活动中使用的类似对话框时,这可能很方便.
| 归档时间: |
|
| 查看次数: |
6412 次 |
| 最近记录: |