简体   繁体   English

选中或取消选中后立即获取复选框状态

[英]Get checkbox status as soon as it is checked or unchecked

I want to get the current status of checkbox as soon as it is check or unchecked. 我想获取复选框处于选中状态或未选中状态的当前状态。 Based on that show the Toast message. 在此基础上显示Toast消息。 Here is my code so far. 到目前为止,这是我的代码。

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean Alarm = getPrefs.getBoolean("cbAlarm", true);
    if(Alarm == true) {
        Toast.makeText(this, "Checked", Toast.LENGTH_LONG).show();
    } else if(Alarm == false) {
        Toast.makeText(this, "UnChecked", Toast.LENGTH_LONG).show();
    }

How can I achieve this. 我该如何实现。 Please help. 请帮忙。

Edit here is my xml file 在这里编辑是我的xml文件

<CheckBoxPreference
    android:title="Enable/Disable Alarm"
    android:defaultValue="true"
    android:key="cbAlarm"
    android:summary="Enable or disable alarm" />

Just implement OnCheckedChangeListener: 只需实现OnCheckedChangeListener即可:

CheckBox myCheckBox = (CheckBox)  findViewById(R.id.my_checkbox);

myCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
        if (isChecked){
             Toast.makeText(this, "Checked", Toast.LENGTH_LONG).show();
        }else{
             Toast.makeText(this, "UnChecked", Toast.LENGTH_LONG).show();
        }  
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM