1

我之前也已经对此进行了 pst,但不知何故,这被冻结了。所以做一个重复的。

我正在编写一个简单的脚本,其中有两个选项的确认框。我需要在一项活动中多次调用它。所以我做了一个方法来做到这一点。基于返回的布尔值,我想编写条件语句。

// Before oncreate

静态布尔确认;

 private void showConfirmation() {
        // UserFunctions userFunctions = null;
        // TODO Auto-generated methodastub

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ProfileActivity.this);

        // set title
        alertDialogBuilder.setTitle("test");
        // set dialog message
        alertDialogBuilder.setMessage("Please update the unfilled fields.").setCancelable(false)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.cancel();
                        confirmation = false;

                    }
                }).setNegativeButton("Later on", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                        confirmation = true;
                    }
                });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();

}    









public void myOnClicktest(View v) {

    showConfirmation();
    if (confirmation){ 

    Intent intent = new Intent(getApplicationContext(), NewActivity.class);
    startActivity(intent);
    }
}
4

1 回答 1

1

这样做-

 alertDialogBuilder.setMessage("Please update the unfilled fields.").setCancelable(false)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {

                        dialog.cancel();
                        confirmation = false;

                    }
                }).setNegativeButton("Later on", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        Intent intent = new Intent(getApplicationContext(),   NewActivity.class);
                        startActivity(intent);
                    }
                });

因为在您的情况下,您的代码不会等待对话框中的确认值更改它会跳过对话框,默认情况下确认值为 false。

于 2013-09-01T09:25:04.330 回答