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 conditionver != 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

String2 つの を!=演算子と比較しているようです。Java で文字列を比較する適切な方法は、.equals(String string)関数を使用することです。

比較はおそらく次のようになります。

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