Java Android 警报对话框并设置正面按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19286135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:42:47  来源:igfitidea点击:

Android alert dialog and set positive button

javaandroideclipse

提问by user1287230

This is for a slider puzzle. I want to show a dialog box with OK button when the puzzle is completed. When the OK button is pressed, I use an Intentto load a website via Android browser. Only problem is that with the current code, when the puzzle is complete, it does not load a box (it does when I use null). It doesn't do anything. Any ideas?

这是一个滑块拼图。我想在拼图完成后显示一个带有 OK 按钮的对话框。当按下 OK 按钮时,我使用 anIntent通过 Android 浏览器加载网站。唯一的问题是,对于当前的代码,当拼图完成时,它不会加载一个盒子(当我使用 时会加载null)。它什么也不做。有任何想法吗?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(!puzzle.isSolved() ? R.string.title_stats : stats.isNewBest() ? R.string.title_new_record : R.string.title_solved);
builder.setMessage(msg);
builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
        Bundle b = new Bundle();
        b.putBoolean("new_window", true); //sets new window
        intent.putExtras(b);
        startActivity(intent);
     }
});

采纳答案by Sino

AlertDialog.Builder builder = new AlertDialog.Builder(your_activity.this);
builder.setTitle(!puzzle.isSolved() ? R.string.title_stats : stats.isNewBest() ? R.string.title_new_record : R.string.title_solved);
builder.setMessage(msg);
builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
        Bundle b = new Bundle();
        b.putBoolean("new_window", true); //sets new window
        intent.putExtras(b);
        startActivity(intent);
     }
});
builder.show();

try this

尝试这个

回答by Sino

Add following code to show the dialog.

添加以下代码以显示对话框。

AlertDialog alert = builder.create();
alert.show();

回答by Ameer

Check the below code. It may help you

检查下面的代码。它可能会帮助你

AlertDialog alertDialog = new AlertDialog.Builder(
                GeneralClassPhotoCaptureImageVideo.this).create(); // Read
                                                                    // Update
        alertDialog.setTitle("Title of dialog");
        alertDialog
                .setMessage("contents");
        alertDialog.setButton(Dialog.BUTTON_POSITIVE, "Ok",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                      Intent intent = new Intent("android.intent.action.VIEW", Uri.parse("http://www..com"));
    Bundle b = new Bundle();
    b.putBoolean("new_window", true); //sets new window
    intent.putExtras(b);
    startActivity(intent);
                    }


                });
        alertDialog.setButton(Dialog.BUTTON_NEGATIVE, "Cancel",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub


                    }
                });
        alertDialog.show();