3

我的自定义偏好类有助于颜色选择。
自定义首选项调用 GridView Intent 它包含 9 种颜色。
但是我的自定义类无法刷新用户在 gridview 上选择的颜色

当我完成首选项意图,然后重新启动首选项时,
颜色会在我选择时显示我。

onBindView()方法,我过头了。这是完美的。
但我不知道我必须重写什么方法来刷新颜色。

在此处输入图像描述


public class ConfigImage extends Preference
{
    Context mContext;

    public ConfigImage(Context context, AttributeSet attrs)
    {
        this(context, attrs, 0);
        this.mContext = context;
    }

    public ConfigImage(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        this.mContext = context;
        setLayoutResource(R.layout.preference_image);
    }

    @Override
    public void onBindView(View view)
    {
        super.onBindView(view);
        ImageView imageView = (ImageView) view.findViewById(R.id.preference_image_iv_color);
        if (imageView != null)
        {
            SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(mContext);
            int colorIndex = sharedPref.getInt("setting_color_default", 3);
            imageView.setBackgroundColor(Color.parseColor(ColorStorage.colors[colorIndex]));
        }
    }
}

.

    <PreferenceCategory
    android:key="setting_color_info"
    android:title="색상" >

    <com.pack.ConfigImage
        android:id="@+id/setting_color_default"
        android:key="setting_color_default"
        android:selectable="true"
        android:summary="Choose default color"
        android:title="Default Color"
        settings:background="#FFFFFFFF" />
</PreferenceCategory>
4

1 回答 1

0

假设您的问题没有收到共享偏好更改的通知,那么您应该订阅它的更改,例如

registerOnSharedPreferenceChangeListener(this);
于 2013-04-01T09:08:43.113 回答