我想在我的应用程序中添加免责声明,该免责声明会在应用程序的首次启动时弹出。如果用户拒绝,该应用程序将关闭。如果用户再次打开应用程序,则免责声明应再次弹出,直到用户接受为止。一旦接受,它应该隐藏并且不再弹出。

我的问题:如果我接受了免责声明,它将关闭并且一切都很好。
但是,当我启动该应用程序时,它再次弹出。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final SharedPreferences pref = getSharedPreferences("Preferences", MODE_PRIVATE);

    String lver = pref.getString("Version", "");
    String ver = this.getString(R.string.version);
    if(ver != lver)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Disclaimer")
            .setMessage(this.getString(R.string.disclaimer))
            .setCancelable(false)
            .setIcon(R.drawable.caution)
            .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                SharedPreferences.Editor edit = pref.edit();
                public void onClick (DialogInterface dialog, int id) {

                    boolean accepted = true;
                    dialog.cancel();
                    if(accepted == true)
                    {
                    edit.putString("Version", this.getString(R.string.version));
                    edit.commit();}
                }
                private String getString(int version) {
                    // I had to create this method, cause i got an error the line above this.getstring(R.string.version)
                    return null;
                }
            })
            .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    MainActivity.this.finish();
                }
            });
        AlertDialog disc = builder.create();
        disc.show();
 } }


我找到了quite similar question,但无法解决它的问题。

对于答案,以及代码中的详细解释,我将感到非常高兴。因为我想了解更多/为什么它可以解决我的问题,并且不想复制和插入代码,并且对它的工作感到高兴。

最佳答案

您的问题是if条件ver != lver,因为您应检查Strings是否等于equals。

if(ver.equals(lver))


因为已经多次讨论了这个问题,所以这里的link应该可以很好地解释它。

进一步更改此部分(我在代码中添加了注释)

    // this doesn't belong here. get it in onClick of the positive Button
    final SharedPreferences pref = getSharedPreferences("Preferences", MODE_PRIVATE);
    builder.setTitle("Disclaimer")
        .setMessage(this.getString(R.string.disclaimer))
        .setCancelable(false)
        .setIcon(R.drawable.caution)
        .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
            SharedPreferences.Editor edit = pref.edit(); // the same as with the pef object
            public void onClick (DialogInterface dialog, int id) {

                boolean accepted = true; // no need for this because the if clause will always be true
                dialog.cancel(); // cancel the dialog IF the job here is done
                if(accepted == true)
                {
                edit.putString("Version", this.getString(R.string.version)); // delete the this pointer
                edit.commit();}
            }
            // no need for this method
            private String getString(int version) {
                // I had to create this method, cause i got an error the line above this.getstring(R.string.version)
                return null;
            }
        });
        .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                MainActivity.this.finish();
            }
        });


对此

    builder.setTitle("Disclaimer")
        .setMessage(this.getString(R.string.disclaimer))
        .setCancelable(false)
        .setIcon(R.drawable.caution)
        .setPositiveButton("Accept", new DialogInterface.OnClickListener() {

            public void onClick (DialogInterface dialog, int id) {
                SharedPreferences pref = getSharedPreferences("Preferences", MODE_PRIVATE);
                SharedPreferences.Editor edit = pref.edit();
                edit.putString("Version", getString(R.string.version));
                edit.commit();
                dialog.cancel();
            }

        });
        .setNegativeButton("Decline", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                MainActivity.this.finish();
            }
        });


现在应该没事

关于java - 每次运行时都会显示alertDialog免责声明,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18747613/

10-10 17:01