我必须在preferncescreen中具有许多checkboxpreference,并且必须注册每个已更改的checkbox值,并根据其打开或关闭来执行一些任务。如何在checkboxpreference中执行此操作?
我知道使用以下代码在复选框中执行此操作:

    public void onCheckboxClicked(View view) {
            // Is the view now checked?
        boolean checked = ((CheckBox) view).isChecked();

        // Check which checkbox was clicked
        switch(view.getId()) {
            case R.id.Activate:
            if (checked){
                // Put some meat on the sandwich
                Toast.makeText(Daydream.this, "checked!", Toast.LENGTH_LONG).show();
            }else
                // Remove the meat
                Toast.makeText(Daydream.this, "UNchecked!", Toast.LENGTH_LONG).show();
            break;
        case R.id.sencond_id:
            if (checked){
                // Cheese me
            }else
                // I'm lactose intolerant
            break;
        // TODO: Veggie sandwich
    }
}


但是我不知道要用checkboxpreference来做到这一点,有什么办法可以做上面的事吗?谢谢

最佳答案

用这样的东西

final Preference otherpref = (Preference) findPreference("otherpref");
final Preference pref = (Preference) findPreference("checkbox");

pPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {

public boolean onPreferenceClick(Preference preference) {

    Toast.makeText(getBaseContext(), "Some text", Toast.LENGTH_SHORT).show();
    return true;
}
});

otherpref .setOnPreferenceClickListener(new OnPreferenceClickListener() {

public boolean onPreferenceClick(Preference preference) {

    Toast.makeText(getBaseContext(), "Other text", Toast.LENGTH_SHORT).show();
    return true;
}
});

关于android - Checkboxpreference监听器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17271717/

10-11 20:38