我想在应用的第一次启动时弹出一个免责声明。如果用户拒绝,应用程序将关闭。如果用户再次打开应用程序,则应再次弹出免责声明,直到用户接受为止。一旦被接受,它应该隐藏,不再弹出。
我的问题:如果我接受了免责声明,它会关闭,一切都很好。 但是当我启动应用程序时,它会再次弹出。
@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,但我无法解决我的问题。
我会对答案感到高兴,如果可能的话,我会在代码中给出一个很好的解释。因为我想了解更多/为什么它解决了我的问题而且不想复制&插入代码并对它的工作感到高兴。
答案 0 :(得分:4)
您的问题是if条件ver != lver
,因为您应该使用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();
}
});
现在应该没问题
答案 1 :(得分:2)
看起来您正在将两个String
与!=
运算符进行比较。在Java中比较字符串的正确方法是使用.equals(String string)
函数。
您的比较应该如下所示:
if(!ver.equals(lver)){
...
}
答案 2 :(得分:0)
**谢谢大家的答案...... 但根据我的关注,免责声明对话框应该是这样的
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>*