开发者

Create AlertDialog in Android on a class that extends SurfaceView

开发者 https://www.devze.com 2023-02-07 02:15 出处:网络
Hey, So I have searched the internet high and low for this. Whenever, someone makes a dialog they do it on a class extending Activity.

Hey, So I have searched the internet high and low for this. Whenever, someone makes a dialog they do it on a class extending Activity. So I have a开发者_JAVA技巧 class extends SurfaceView and I need to show a dialog every time the player in my game destroys a certain number of enemies.

How do I do this? I keep getting errors on the code I copy from online.

This is my class :-

class SurvivorPanel extends SurfaceView implements SurfaceHolder.Callback 

This is my constructor:-

 public SurvivorPanel(Context context) { // set panel's holder & thread
    super(context);
    getHolder().addCallback(this);
    _thread = new TutorialThread(getHolder(), this);
    setFocusable(true);
}

Some one please advise me how to create an AlertDialog here....


Just use the context that you received in your constructor.

With regards to your question... you can divide it. You can put everything except the dialog.show() in the constructor; then you can execute dialog.show() and dialog.dismiss() elsewhere:

private AlertDialog dialog;

public Constructor(Context context){
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    dialog = builder.setTitle("The title")
        .setMessage("The content")
        .create();
    dialog.show();
}

public void someWhere(){
    dialog.show();
    // or when you want to close it:
    dialog.dismiss();
}
0

精彩评论

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