CheckBox cb = (CheckBox) findViewById(R.id.cb);//cb是CheckBox的id
//判断选框是否被勾选
if (cb.isChecked()){
//true 执行
}
/*
判断当前的勾选装填
*/
public boolean isChecked(){
return cbStatus.isChecked();
}
public void setChecked(boolean check){
cbStatus.setChecked(check);
}
private SettingItemView sivupdate;
private SharedPreferences mPref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
//sp设置名字config,MODE_PRIVATE私有
mPref = getSharedPreferences("config", MODE_PRIVATE);
sivupdate = (SettingItemView) findViewById(R.id.siv_update);
sivupdate.setTitle("自动更新设置");
boolean autoupdate = mPref.getBoolean("auto_update", true);
if(autoupdate){
sivupdate.setDesc("总共更新以开启");
sivupdate.setChecked(true);
}else{
sivupdate.setDesc("总共更新以关闭");
sivupdate.setChecked(false);
}
设置监听
sivupdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//判断当前的勾选状态 ,在自定义的view,里面自定义方法
if (sivupdate.isChecked()){
//设置不够选
sivupdate.setChecked(false);
sivupdate.setDesc("自动更新已关闭");
//更新sp
mPref.edit().putBoolean("auto_update",false).commit();
}else {
//设置够选
sivupdate.setChecked(true);
sivupdate.setDesc("总共更新以开启");
mPref.edit().putBoolean("auto_update",true).commit();
}
}
});
设置CheckBox不能被点击
android:contextClickable=”false”