簡體   English   中英

Android:SharedPreferences無法正常工作

[英]Android : SharedPreferences is not working

我正在嘗試實現SharedPreferences 我已經搜索了它,嘗試了將近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();

    }
}

我知道我的SharedPreferences值保持True 所以我的問題是,當我打開相同的Activity時如何使其不成立?

如果有人可以從我的代碼中給我一個示例,我真的很高興。 所以我可以從中學到東西。

非常感謝。

我會拋出你的聲明:

editor.putBoolean("banner_pref", true);

到您的else語句中。 如果您的if被擊中,這將阻止在更改它之前對其進行設置。

我剛剛對您的代碼做了一些修復,它應該像這樣工作

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM