首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么未被检查的状态不被保存?

为什么未被检查的状态不被保存?
EN

Stack Overflow用户
提问于 2017-02-27 05:59:27
回答 1查看 44关注 0票数 2

我有一个活动,其中有一个checkBox,现在出现的问题是,当checkBox处于选中状态时,它仍然没有问题,但是当我取消检查时,它会恢复到其检查状态。为什么会发生这种情况?这是我的代码,我使用了共享偏好。

代码语言:javascript
复制
 public class Kinematics extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, CompoundButton.OnCheckedChangeListener {
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    private ActionBarDrawerToggle phy_law_toggel;
    private Toolbar toolbar;
    private WebView webView;
    private FloatingActionButton floatingActionButton;
    private RelativeLayout relativeLayout;
    private CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kinematics);
        Toolbar toolbar = (Toolbar) findViewById(R.id.phy_lawtoolbar);
        setSupportActionBar(toolbar);
        setTitle(R.string.Kinematics);
        relativeLayout = (RelativeLayout) findViewById(R.id.relative);
        drawerLayout = (DrawerLayout) findViewById(R.id.phy_draw);
        navigationView = (NavigationView) findViewById(R.id.nav_view_phy);
        phy_law_toggel = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.Close);
        drawerLayout.addDrawerListener(phy_law_toggel);
        toolbar = (Toolbar) findViewById(R.id.phy_lawtoolbar);
        setSupportActionBar(toolbar);
        phy_law_toggel.syncState();
        checkBox = (CheckBox) findViewById(R.id.fav);
        checkBox.setChecked(getAsp("checkbox"));
        checkBox.setOnCheckedChangeListener(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        navigationView.setItemIconTintList(null);
        webView = (WebView) findViewById(R.id.phy_law_web);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setBuiltInZoomControls(false);
        webView.loadUrl("file:///android_asset/mathscribe/Kinematics.html");
        floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.scrollTo(0, 0);
            }
        });
        navigationView.setNavigationItemSelectedListener(this);
        DataBaseHandler db = new DataBaseHandler(this);
        if (checkBox.isChecked()) {
            db.add_activity(this.getClass().getSimpleName());

        }


    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (phy_law_toggel.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.favourite) {
            startActivity(new Intent(getApplicationContext(), FavoritePage.class));
        }
        drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    }


    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (checkBox.isChecked()) {
            Toast.makeText(getApplicationContext(), " checked",
                    Toast.LENGTH_LONG).show();
            saveInSp("checkbox", isChecked);
        } else if (!checkBox.isChecked()) {
            Toast.makeText(getApplicationContext(), "not checked",
                    Toast.LENGTH_LONG).show();
            saveInSp("checkbox", !isChecked);
        }

    }

    private boolean getAsp(String key) {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("ALpit", android.content.Context.MODE_PRIVATE);
        return sharedPreferences.getBoolean(key, false);
    }

    private void saveInSp(String key, boolean value) {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("ALpit", android.content.Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(key, value);
        editor.apply();
    }


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-27 18:53:20

只要去掉'!‘在onCheckedChange中被选中,它将完美无缺地工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42478764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档