返回 bool 值的 Android alertdialog 生成器

标签 android

我之前也已经 pst 这个了,但不知何故它被卡住了。所以做一个重复的。

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

// Before oncreate

静态 bool 确认;

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

最佳答案

这样做-

 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。

关于返回 bool 值的 Android alertdialog 生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18556938/

相关文章:

android - View 类型中的方法 setOnLongClickListener(View.OnLongClickListener) 不适用于参数(new OnLongClickListener(){})

Android Studio imageButton 和切换到另一个 Activity

android - 保存叠加图像 + cameraPreview

java - 在首次加载 ViewPager 时显示内容

java - 如何将图像从Android客户端传输到Java服务器

Android - 在 SettingsActivity 屏幕底部包含一个 ButtonBar?

java - 如何从 Alert.Builder 中的多选项目中获取选中的项目?

java - 未找到默认的 RealmConfiguration。调用 setDefaultConfiguration()

Android 4.3 及更高版本背景图片不显示

Android Studio : Files are edited without my intervention