如何将按钮状态保存到Android

时间:2016-09-25 14:31:11

标签: java android sharedpreferences android-sharedpreferences

我想创建内容应用程序,我从 JSON 加载数据,当用户点击button时,将此内容保存到 SQLitedatabase
我想用this library作为按钮 对于可检查按钮,我使用this代码

我写下面的代码,点击按钮后我将此内容保存到SQLitedatabase,但我无法将按钮状态保存到 SharedPreferences

点击按钮(boolean checked)按钮开启,然后再次点击按钮关闭此按钮。
<登记/> 我想点击按钮,打开此按钮并保存在SharedPreferences,当转到其他activity并再次返回此activity时,请参阅开启按钮不关闭。当再次点击此按钮时关闭按钮!

活动代码:

private ShineButton postShow_favPost;
private String favData = "FavPrefsList";
private Boolean favState;

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.post_show_page);

    bindActivity();

    //Give Data
    Bundle bundle = getIntent().getExtras();

    if (bundle != null) {
        title = bundle.getString("title");
        image = bundle.getString("image");
        content = bundle.getString("content");
        dateTime = bundle.getString("dateTime");
        author = bundle.getString("author");
        category = bundle.getString("category");
        categoryID = bundle.getString("categoryID");
    }

    mAppBarLayout.addOnOffsetChangedListener(this);

    //// Save Fav state
    final SharedPreferences saveFavPrefs = getSharedPreferences(favData, MODE_PRIVATE);
    final SharedPreferences.Editor editor = saveFavPrefs.edit();

    favState = saveFavPrefs.getBoolean("isChecked", false);

    postShow_favPost = (ShineButton) mToolbar.findViewById(R.id.post_FavImage);
    postShow_favPost.init(this);
    postShow_favPost.setOnCheckStateChangeListener(new ShineButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(View view, boolean checked) {

            if (checked == true) {
                editor.putBoolean("isChecked", true);
                editor.commit();

                //////////// Database
                favDB = new FavHelper(context);

                long addNewFAV = favDB.insertFAV(title, image, content, dateTime, author, category);

                if (addNewFAV < 0) {
                    TastyToast.makeText(context, "Not save in database", TastyToast.LENGTH_LONG, TastyToast.ERROR);
                } else {
                    TastyToast.makeText(context, "Save in database", TastyToast.LENGTH_LONG, TastyToast.SUCCESS);
                }

                ////////////////////
            } else {
                editor.putBoolean("isChecked", false);
                editor.commit();
                Toast.makeText(context, "Checked False", Toast.LENGTH_SHORT).show();
            }

        }
    });

如何解决我的问题?

1 个答案:

答案 0 :(得分:0)

您需要在单击按钮时保存状态。 例如:

postShow_favPost.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (postShow_favPost.isChecked()){
            editor.putBoolean("isChecked", true);
            editor.apply();
     }
     else{
         editor.putBoolean("isChecked", false);
         editor.apply();
      }
    }
});

然后您必须将其加载到onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    SharedPreferences saveFavPrefs = getSharedPreferences(favData, MODE_PRIVATE);;
    postShow_favPost.setChecked(saveFavPrefs.getBoolean("isChecked", true));
}

也许这个例子可以帮到你。

修改

好吧,让我们假设您的f函数刚刚完成了对数据库所做的任何操作,然后将状态设置为 on off ,假设你想要

所以:

public void loadPrefs(SharedPreferences prefs){
    favState = prefs.getBoolean("isChecked",false);
}


public void f(SharedPreferences.Editor editor){
//database stuff
 editor.putBoolean("isChecked", true); // or false
 editor.apply();

 loadPrefs(prefs); 
  

//在这里,您的状态将立即保存为您的按钮   在你的onResume()之后完成数据库的使用   如果您需要just for that post

,可以将该状态更改为false(关闭)
}