创建自定义首选项?教程?

时间:2015-08-07 07:48:09

标签: java android android-preferences

2Dee告诉我,我的问题不符合规则。让我重新尝试一下;)

  

What topics can I ask about here?

     
      
  1. 要求我们推荐或查找书籍,工具,软件库,教程或其他场外资源的问题都是Stack Overflow的主题,因为它们往往会吸引固执己见的答案和垃圾邮件。相反,描述问题以及到目前为止已经做了什么来解决它。
  2.   

我需要将以下功能添加到我的自定义首选项中:

  • [1]以编程方式编辑或禁用标题颜色
  • [2]使图标颜色以编程方式可编辑或禁用
  • [3]在对话框中添加确定按钮

IMG 镜像:1234567

必须添加到MultiIconListPreference-Class的内容?我无法找到一般性亵渎的指南。这就是为什么要求非专业化的原因。

我卡住的地方:

// R.styleable.IconPreference +
// R.styleable.IconPreference_icon1 +
// R.styleable.IconPreference_entryIcons1 (res/values/attrs.xml)
<resources>
    <declare-styleable name="IconPreference">
        <attr name="icon1" format="reference" />
        <attr name="entryIcons1" format="reference" />
    </declare-styleable>
</resources>

// res/layout/list_item_dialog_pref.xml
<LinearLayout
    android:id="@+id/image_list_view_row_table_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:weightSum="1"
    android:paddingLeft="6dp"
    android:paddingStart="6dp"
    android:paddingRight="6dp"
    android:paddingEnd="6dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/list_item_dialog_pref_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="?android:textColorPrimary"
        android:layout_weight="0.95"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:layout_margin="2dp"/>

    <android.support.v7.widget.AppCompatCheckBox
        android:id="@+id/list_item_dialog_pref_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

// res/layout/preference_icon.xml
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:padding="?android:attr/scrollbarSize">

    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="6dp"
        android:layout_marginTop="6dp"
        android:layout_weight="1">

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:textColor="?attr/android:textColorPrimary"
            android:textSize="16sp" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="2"
            android:textSize="14sp" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginRight="3dp"/>
</LinearLayout>

// res/xml/preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory>
        <com.android.app.MultiIconListPreference
            android:key="item_key"
            android:summary="@string/item_key_sum"
            android:title="@string/item_key"/>
    </PreferenceCategory>
</PreferenceScreen>

// com.android.app.MultiIconListPreference.java
public class MultiIconListPreference extends ListPreference {
    private Drawable mIcon;
    private IconListPreferenceScreenAdapter iconListPreferenceAdapter = null;
    private Context mContext;
    private LayoutInflater mInflater;
    private CharSequence[] entries;
    private CharSequence[] entryValues;
    private int[] mEntryIcons = null;
    private SharedPreferences prefs;
    private SharedPreferences.Editor editor;
    private String mKey;
    private int selectedEntry = -1;

    public MultiIconListPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MultiIconListPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs);
        setLayoutResource(R.layout.preference_icon);
        mContext = context;

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconPreference, defStyle, 0);
        mIcon = a.getDrawable(R.styleable.IconPreference_icon1);

        int entryIconsResId = a.getResourceId(R.styleable.IconPreference_entryIcons1, -1);
        if (entryIconsResId != -1) {
            setEntryIcons(entryIconsResId);
        }
        mInflater = LayoutInflater.from(context);
        mKey = getKey();
        prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
        editor = prefs.edit();

        a.recycle();
    }

    @Override
    public String getValue() {
        if (selectedEntry != -1)
            return entryValues[selectedEntry].toString();
        return super.getValue();
    }

    @Override
    public void onBindView(View view) {
        super.onBindView(view);
        ImageView imageView = (ImageView) view.findViewById(R.id.icon);
        if (imageView != null && mIcon != null) {
            imageView.setImageDrawable(mIcon);
        }
    }

    public void setIcon(Drawable icon) {
        if ((icon == null && mIcon != null) || (icon != null && !icon.equals(mIcon))) {
            mIcon = icon;
            notifyChanged();
        }
    }

    public Drawable getIcon() {
        return mIcon;
    }

    public void setEntryIcons(int[] entryIcons) {
        mEntryIcons = entryIcons;
    }

    public void setEntryIcons(int entryIconsResId) {
        TypedArray icons_array = mContext.getResources().obtainTypedArray(entryIconsResId);
        int[] icon_ids_array = new int[icons_array.length()];
        for (int i = 0; i < icons_array.length(); i++) {
            icon_ids_array[i] = icons_array.getResourceId(i, -1);
        }
        setEntryIcons(icon_ids_array);
        icons_array.recycle();
    }

    @Override
    protected void onPrepareDialogBuilder(Builder builder) {
        super.onPrepareDialogBuilder(builder);

        entries = getEntries();
        entryValues = getEntryValues();

        if (entries.length != entryValues.length) {
            throw new IllegalStateException("ListPreference requires an entries array and an entryValues array which are both the same length");
        }

        if (mEntryIcons != null && entries.length != mEntryIcons.length) {
            throw new IllegalStateException("MultiIconListPreference requires the icons entries array be the same length than entries or null");
        }

        iconListPreferenceAdapter = new IconListPreferenceScreenAdapter(mContext);

        if (mEntryIcons != null) {
            String selectedValue = prefs.getString(mKey, "");
            for (int i = 0; i < entryValues.length; i++) {
                if (selectedValue.compareTo((String) entryValues[i]) == 0) {
                    selectedEntry = i;
                    break;
                }
            }
            builder.setAdapter(iconListPreferenceAdapter, null);

        }
    }

    private class IconListPreferenceScreenAdapter extends BaseAdapter {
        public IconListPreferenceScreenAdapter(Context context) {

        }

        public int getCount() {
            return entries.length;
        }

        class CustomHolder {
            private TextView text = null;
            private AppCompatCheckBox rButton = null;

            CustomHolder(View row, int position) {
                text = (TextView) row.findViewById(R.id.list_item_dialog_pref_textview);
                text.setText(entries[position]);

                rButton = (AppCompatCheckBox) row.findViewById(R.id.list_item_dialog_pref_checkbox);
                rButton.setId(position);
                rButton.setClickable(false);
                rButton.setChecked(selectedEntry == position);

                if (mEntryIcons != null) {
                    text.setText(" " + text.getText());
                    text.setCompoundDrawablesWithIntrinsicBounds(mEntryIcons[position], 0, 0, 0);
                }
            }
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            CustomHolder holder = null;
            final int p = position;
            row = mInflater.inflate(R.layout.list_item_dialog_pref, parent, false);
            holder = new CustomHolder(row, position);

            row.setTag(holder);

            // row.setClickable(true);
            // row.setFocusable(true);
            // row.setFocusableInTouchMode(true);
            row.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    v.requestFocus();

                    Dialog mDialog = getDialog();
                    mDialog.dismiss();

                    MultiIconListPreference.this.callChangeListener(entryValues[p]);
                    editor.putString(mKey, entryValues[p].toString());
                    selectedEntry = p;
                    editor.commit();
                    editor.apply();

                }
            });

            return row;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

嗯,我想我能够自己回答这个问题:) 我在GitHub上找到material-dialogs并在我的应用程序的其他部分使用它。
例如过滤器: Create PreferenceDialog onClick on filter popup?

我很快就会加上详细的解释。