CheckBox判断是否勾选

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”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值