Android关闭PopUp

时间:2016-03-23 17:50:54

标签: android popup hide dismiss

如何在点击按钮时关闭PopUp?

.dismiss();.cancel();对我不起作用,或者我做错了。

    //Game Paused PopUp
private void PauseMenu() {

    final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);

    LayoutInflater inflater = getLayoutInflater();
    final View popupView = inflater.inflate(R.layout.pause_menu, null);
    helpBuilder.setView(popupView);
    Button resume = (Button) popupView.findViewById(R.id.resume);

    resume.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //Close PopUp
        }
    });

    AlertDialog helpDialog = helpBuilder.create();
    helpDialog.show();
}

@Override
public void onBackPressed() {
    PauseMenu();
}

感谢您的帮助:)

请不要在没有评论的情况下投票!

2 个答案:

答案 0 :(得分:1)

您需要保留对该对话框的引用才能将其解除。

以下内容应该有效:

private void displayPauseMenu() { // method names are always lower-case!!

    final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);

    LayoutInflater inflater = getLayoutInflater();
    final View popupView = inflater.inflate(R.layout.pause_menu, null);
    helpBuilder.setView(popupView);

    // this is what you need to dismiss
    final AlertDialog helpDialog = helpBuilder.create();
    Button resume = (Button) popupView.findViewById(R.id.resume);

    resume.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            helpDialog.dismiss();
        }
    });

    helpDialog.show();
}

@Override
public void onBackPressed() {
    displayPauseMenu();
}

答案 1 :(得分:0)

private void PauseMenu() {

    final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);

    LayoutInflater inflater = getLayoutInflater();
    final View popupView = inflater.inflate(R.layout.pause_menu, null);
    helpBuilder.setView(popupView);
    Button resume = (Button) popupView.findViewById(R.id.resume);

    final AlertDialog helpDialog = helpBuilder.create();
    resume.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            helpDialog.dismiss();
        }
    });

    helpDialog.show();
}

@Override
public void onBackPressed() {
    PauseMenu();
}