1

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

    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 来做这件事。有什么办法可以像上面那样做吗?我知道preferencescreen 已被弃用,但我必须使用它,因为它很简单,可以制作两行 listview。我希望我能尽快得到答复。谢谢

4

1 回答 1

5

像这样使用它

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; 
}
});
于 2013-06-24T09:13:46.477 回答