我想这样做:当 CheckBoxPreference 未选中时,CheckBoxPreference 标题的文本颜色变为灰色,如果选中,标题的文本颜色将恢复为原始颜色(取决于主题)。
到目前为止我所做的:我创建了一个从CheckBoxPreference
.
public class CustomCheckBoxPreference extends CheckBoxPreference{
TextView txtTitle;
int originalTextColor;
public CustomCheckBoxPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onBindView(View view) {
txtTitle = (TextView) view.findViewById(android.R.id.title);
originalTextColor = txtTitle.getCurrentTextColor();
setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
if (isChecked()) {
txtTitle.setTextColor(originalTextColor); //it doesn't work
}
else {
txtTitle.setTextColor(Color.GRAY); //it doesn't work
}
return true;
}
});
super.onBindView(view);
}
}
当我运行应用程序时,txtTitle.setTextColor(..)
显然没有工作,文本颜色根本没有改变。我还通过调试器确认onPreferenceClick
调用了该方法。