我有一个活动,其中有一个checkBox,现在出现问题,当checkBox处于检查状态时,它仍然检查没有问题, 但是当我取消选中它时,它会恢复到检查状态。 为什么会这样? 在这里,是我的代码,我使用了共享首选项。
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();
}
}
答案 0 :(得分:1)
只需删除'!'在onCheckedChange中的!缺陷,它将完美地工作。