1

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

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

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

我发现了一个非常相似的问题,但我无法用它解决我的问题。

我会很高兴得到一个答案,如果可能的话,我会在代码中给出一个很好的解释。因为我想了解更多/为什么它解决了我的问题并且不想复制和插入代码并且很高兴它可以工作。

4

3 回答 3

4

您的问题是 if 条件ver != lver,因为您应该检查字符串是否与 equals 相等。

if(ver.equals(lver))

因为这个问题已经讨论过很多次了,所以这里有一个链接应该可以很好地解释它。

此外更改这部分(我在代码中注释)

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

现在应该没问题

于 2013-09-11T17:43:20.483 回答
2

看起来您正在将两个String' 与!=运算符进行比较。在 Java 中比较字符串的正确方法是使用该.equals(String string)函数。

您的比较应该如下所示:

if(!ver.equals(lver)){
    ...
}
于 2013-09-11T17:43:07.933 回答
0

**Thank you All for Answers... but As per my concern Disclaimer dialog should be look like enter image description here

 i have customise the Dialog and used the same logic of SharedPrefence to save the users choice.
    The source code are ---
   ****MainActivity.java ***** 
    public class MainActivity extends Activity {
        SharedPreferences   prefs ;

        boolean isShowAgain=true;
        boolean accepted=false;
        CheckBox dialogcb;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            prefs = getSharedPreferences("detail", MODE_PRIVATE);
            boolean isshowagain =prefs.getBoolean("show_again", true);
             if(isshowagain)
                showdialog();
        }
        private void showdialog() {
            final Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.custom_dialog);
            dialog.setTitle("Disclaimer...");
            Button dialogButton = (Button) dialog.findViewById(R.id.button1);
           dialogcb= (CheckBox) dialog.findViewById(R.id.checkBox1);
            dialogButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                        accepted = dialogcb.isChecked();
                        SharedPreferences.Editor edit=prefs.edit();
                        edit.putBoolean("show_again",!accepted);
                        edit.commit();
                       dialog.dismiss();
                }
            });
            dialog.show();
        }
    }*


    and the custom_dialog.xml is---
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_marginBottom="10dip"
            android:layout_marginLeft="20dip"
            android:layout_marginRight="20dip"
            android:layout_marginTop="10dip"
            android:text="This is alert dialog will run  every time when App will launch .
            if you want to close for always just check on check box and then press agree button" />
        <CheckBox
            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Do not show it Again." />
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dip"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:layout_marginTop="5dip"
            android:text="Agree" />
    </LinearLayout>*
于 2013-09-12T19:08:00.163 回答