开发者

Creating AlertDialog with Static Methods?

I've completed most of the game I'm attempting to make and throughout the project I've created one particular Activity which also cal开发者_运维问答ls 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 :)


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

}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜