简体   繁体   中英

Android alert dialog and set positive button

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 Intent to 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?

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

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

Add following code to show the dialog.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM