使用静态方法创建 AlertDialog?
我已经完成了我尝试制作的大部分游戏,并且在整个项目中我创建了一个特定的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我使用的:
Here is what I used:
SurfaceView 扩展了 View,因此有一个 getContext() 方法
要创建并显示 AlertDialog,您可以在 SurfaceView 中执行以下代码。
如果您的 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
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