复选框在重新打开应用程序时传递动作 [英] Checkbox passing of action on reopening of the app

查看:73
本文介绍了复选框在重新打开应用程序时传递动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用共享首选项存储了复选框值(选中/取消选中),但是在关闭并重新打开应用程序之后/之后通过复选框的操作时遇到了问题。
植入:在不同活动中的按钮在单击复选框时会隐藏/显示(即check = shows& uncheck =隐藏)。
当我关闭应用程序并重新打开时,复选框保持选中状态,但按钮未出现

I have stored the checkbox value(check/uncheck) using shared preference,but I face problem while passing the action of checkbox on/after closing and reopening the app.
Explantion: A button in different actvity hides/shows on clicking the checkbox(i.e check=shows & uncheck= hides) working correctly.
when I close the app and reopen the checkbox stays checked but button is not appearing

使用共享首选项保存的复选框代码

checkbox code saved using shared preference

final CheckBox checkBox = (CheckBox) findViewById(R.id.add_fb);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = preferences.edit();

        checkBox.setChecked(preferences.getBoolean("checked",false));

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isCheckedValue = isChecked;
                editor.putBoolean("checked", isChecked);
                editor.apply();
            }
        });

    }

我试图通过提供以下内容来实现onStart()来传递数据if-else条件

I tried to implement onStart() for passing data by providing if-else condition

@Override
    protected void onStart() {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = preferences.edit();
        super.onStart();
        if(checkBox.isChecked()) {
            editor.putBoolean("checked", true);
            editor.apply();
        }else{
            editor.putBoolean("checked", false);
            editor.apply();
        }
    }

在此复选框中,我将在此处传递数据已选中

This is where i am passing the data once the checkbox is checked

@Override
            public void onBubbleClick(BubbleLayout bubble) {
                Intent in = new Intent(MainActivity.this, PopUpWindow.class);
                in.putExtra("yourBoolName", isCheckedValue );
                startActivity(in);

            }


推荐答案

而不是在'onBubbleClickMethod'中发送'isCheckedValue'
尝试-

Instead of sending 'isCheckedValue' in 'onBubbleClickMethod' try this -

in.putExtra( yourBoolName,preferences.getBoolean( checked,false));

in.putExtra("yourBoolName",preferences.getBoolean("checked",false));

这篇关于复选框在重新打开应用程序时传递动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆