Android警报对话框并设置肯定按钮

时间:2013-10-10 02:38:42

标签: java android eclipse

这是一个滑块拼图。我想在拼图完成时显示一个带OK按钮的对话框。按下确定按钮后,我使用Intent通过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);
     }
});

3 个答案:

答案 0 :(得分:23)

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

试试这个

答案 1 :(得分:5)

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

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

答案 2 :(得分:0)

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

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