Andengine ask questions with toast?

廉价感情. 提交于 2020-01-24 12:29:46

问题


I wonder if there is any native support for andengine or ADK to ask question-toasts? For example if I press the back button, I want some box to popup asking if I really want to quit the application and give me the option to answer yes or no.


回答1:


Better to use alert dialog use this code, hope work same like that

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub

    switch(keyCode)
    {
    case KeyEvent.KEYCODE_BACK:
        AlertDialog.Builder ab = new AlertDialog.Builder(AlertDialogExampleActivity.this);
        ab.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
        .setNegativeButton("No", dialogClickListener).show();
        break;
    }

    return super.onKeyDown(keyCode, event);
}

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which){
        case DialogInterface.BUTTON_POSITIVE:
            //Yes button clicked
            break;

        case DialogInterface.BUTTON_NEGATIVE:
            //No button clicked
            break;
        }
    }
}



回答2:


You found your solution that is good but for the above answer you have to use AndEngine functionality. If you are working with AndEngine then you have to develop all the things with AndEngine power.

So for your solution you have to create one child scene which popup when user press device back button like in the following code snippet.

class DialogBox extends Scene{
    DialogBox(...){
    }
    // you have to include all the functionality that your dialog box should contain
}

You have to set above dialog box as child of your main scene like in the following manner on the back event of user.

mScene.setAsChildScene(new DialogBox(...));

I prefer this way if I am developing a game.



来源:https://stackoverflow.com/questions/16872164/andengine-ask-questions-with-toast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!