使用静态方法创建 AlertDialog?

发布于 2024-09-14 07:51:54 字数 381 浏览 3 评论 0原文

我已经完成了我尝试制作的大部分游戏,并且在整个项目中我创建了一个特定的 Activity,它也调用 SurfaceView 和 Thread。我在 3 个类中的每一个中都放置了一个 update() 方法,这样每次发生变化时,它们都知道其他类在哪里。显然,做这样的事情的唯一方法是使用静态方法...这很好,直到我的 SurfaceView 中发生碰撞并且我想告诉 Activity 做什么。我可以转发信息,但是我找不到制作 AlertDialog 的方法。

我知道我无法从静态方法调用 showDialog(),但我找不到一种方法来创建非静态方法来调用它,然后从静态方法调用该方法。我一直在寻找答案,并且听说过一些有关实例化对象的信息,但我无法弄清楚这意味着什么...

如果有人有好主意让我解决这个问题,请告诉我:)

I've completed most of the game I'm attempting to make and throughout the project I've created one particular Activity which also calls a SurfaceView and a Thread. I put an update() method in each of the 3 classes so they each know where the other ones are everytime something changes. Apparently, the only way to do something like this is using static methods... This is fine until a collision occurs in my SurfaceView and I want to tell the Activity what to do. I can relay the information, but then I cannot find a way to make an AlertDialog.

I understand I cannot call showDialog() from a Static method, but I cannot find a way to make a non-static method to call it with and then call that method from a static one. I've been searching for an answer and I've heard something about instantiating the object but I cannot figure out what that means...

If anyone has a good idea to get me around this, please let me know :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

桃酥萝莉 2024-09-21 07:51:54

这是我使用的:

public static void messageDialog(Activity a, String title, String message){
    AlertDialog.Builder dialog = new AlertDialog.Builder(a);
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setNeutralButton("OK", null);
    dialog.create().show();     

}

Here is what I used:

public static void messageDialog(Activity a, String title, String message){
    AlertDialog.Builder dialog = new AlertDialog.Builder(a);
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setNeutralButton("OK", null);
    dialog.create().show();     

}
路弥 2024-09-21 07:51:54

SurfaceView 扩展了 View,因此有一个 getContext() 方法

要创建并显示 AlertDialog,您可以在 SurfaceView 中执行以下代码。

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("title");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
    }
});
Dialog d = builder.create();
d.show();

如果您的 Activity 重新启动,这可能无法像 Activity.showDialog(int) 一样工作(对话框可能会消失并且您必须自己处理状态)。

希望这有帮助

SurfaceView extends View and thus have a getContext() method

To create and show your AlertDialog, you can do the following code inside your SurfaceView

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("title");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
    }
});
Dialog d = builder.create();
d.show();

This might not work as Activity.showDialog(int) if your activity is restarted (the dialog might simply disappear and you will have to handle state yourself).

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文