Java Android:SharedReferences不工作

Java Android:SharedReferences不工作,java,android,sharedpreferences,Java,Android,Sharedpreferences,我正在尝试实现一个SharedReferences。 我已经搜索它,尝试它,几乎12个小时,它仍然不能帮助我的问题 Java代码 public static final String MyPREFERENCES = "banner_pref" ; SharedPreferences SharedP; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我正在尝试实现一个
SharedReferences
。 我已经搜索它,尝试它,几乎12个小时,它仍然不能帮助我的问题

Java代码

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    SharedPreferences.Editor editor = SharedP.edit();
    editor.putBoolean("banner_pref", true);
    editor.commit();
    Boolean myValue = SharedP.getBoolean("banner_pref", true);

    if (myValue == true){
        bannerfull = (RelativeLayout) findViewById(R.id.banner_full);
        bannerfull.setVisibility(View.VISIBLE);

        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();

    }

    else {

        bannerfull = (RelativeLayout) findViewById(R.id.banner_full);
        bannerfull.setVisibility(View.GONE);


        //SharedP.edit().putBoolean("banner_pref", false).commit();

    }
}
我知道我的
SharedReferences
值是keep
True
。因此,我的问题是,当我打开相同的
活动时,如何使其不正确

如果有人能给我一个代码示例,我真的很感激。所以我可以从中学习


之前非常感谢。

我要抛出你的声明:

editor.putBoolean("banner_pref", true);

进入您的
else
语句。这将防止在点击
(如果
)时在更改之前设置它

我刚刚对你的代码做了一点修改,它应该是这样工作的

* Read the value with default is `true`, if the `SharedPreferences` doesn't exist before, certainly, it is the first time; `true` value is certain.
* if value is `true`, it is time to show the view; and then, update the value to `false` and save to `SharedPreferences`.
* else if value is `false`, hide the view.
* next time, when the `Activity` is launched, the value will be read as `false` (assuming there is none intervention in between, for example, users clear data from `Settings->Apps`...); the view is hidden, assuming there isn't any other action that update value to `true`.
代码如下:

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // load sharedpref value 
    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    Boolean myValue = SharedP.getBoolean("banner_pref", true); // default: true

    // load layout, we just need it anyway
    bannerfull = (RelativeLayout) findViewById(R.id.banner_full);

    // if true, show it
    if (myValue == true) {
        bannerfull.setVisibility(View.VISIBLE);
        // ok, now switch value to 'false' and commit to SharedP
        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();
    }
    // if false, hide it
    else {
        bannerfull.setVisibility(View.GONE);
    }
}
更新:每启动三次显示一次。通过向计数器显示的数量添加新值

public static final String MyPREFERENCES = "banner_pref" ;
SharedPreferences SharedP;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // load sharedpref value 
    SharedP = getSharedPreferences("banner_pref", MainHelloballi.MODE_PRIVATE);
    Boolean myValue = SharedP.getBoolean("banner_pref", true); // default: true
    int counter = SharedP.getInt("counter", 1); // default starting from 1

    if(counter == 3) {
      myValue = true;
      // counter reset
      counter = 0;
    }

    // load layout, we just need it anyway
    bannerfull = (RelativeLayout) findViewById(R.id.banner_full);

    // if true, show it
    if (myValue == true) {
        bannerfull.setVisibility(View.VISIBLE);
        // ok, now switch value to 'false' and commit to SharedP
        editor = SharedP.edit();
        editor.putBoolean("banner_pref", false);
        editor.commit();
    }
    // if false, hide it
    else {
        bannerfull.setVisibility(View.GONE);
    }

    // each time loading, increase the counter
    ++counter;
    editor = SharedP.edit();
    editor.putInt("counter",  counter);
    editor.commit();
}

如果它是
isItFirstTime
您遇到问题(将始终为true),请将它放入if语句中,首先检查它是否尚未设置。然后设置为true,否则设置为false这一行:Boolean myValue=SharedP.getBoolean(“banner\u pref”,true);对于此布尔值myValue=SharedP.getBoolean(“isItFirstTime”,true)@对不起,我弄错了。我编辑code@Doomsknight你能给我举个例子吗?对不起,我问得太多了,谢谢。但是,当我第三次回到活动时,它会再次显示
网络视图
,作为一种解决办法,您可以将
共享引用
存储为另一个布尔值,以说明您的布尔值是否已设置,这样您就可以执行类似
if(oldbooleanhasset){//do something}的操作{//do something}
我以前做过,效果很好,只需在第一个
editor.commit()之后将新的布尔值设置为true
如果,每次我打开应用程序时,视图都会再次显示?呃?只要删除所有与
SharedReferences
相关的部分;视图就会一直显示。不,我的意思是,如果用户重新启动应用程序,视图会再次显示。但是,如果用户只是在应用程序中玩,它不会显示吗?你在开玩笑吧……首先,
它会显示出来ows在应用程序中重新启动
;其次,
用户在应用程序中玩,它不会显示
-逻辑正确吗?等等,让我解释一下。因此,当用户第一次打开应用程序时,网络视图(
就像全屏广告
)将显示。用户按下关闭按钮后,它将不会再次显示,直到用户关闭应用程序并再次打开。