Java 创建自定义首选项?辅导的

Java 创建自定义首选项?辅导的,java,android,android-preferences,Java,Android,Android Preferences,表示我的问题不符合规则。让我再试一次;) 要求我们推荐或查找书籍、工具、软件库、教程或其他非现场资源的问题对于堆栈溢出来说是离题的,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决问题所做的工作 我需要将以下功能添加到我的自定义首选项中: [1]以编程方式编辑或禁用标题颜色 [2]以编程方式编辑或禁用图标颜色 [3]在对话框中添加“确定”按钮 镜像:,, 必须向多图标列表首选项类添加什么?我找不到一本一般性考试的指南。。这就是为什么问得不专业的原因 我卡

表示我的问题不符合规则。让我再试一次;)

  • 要求我们推荐或查找书籍、工具、软件库、教程或其他非现场资源的问题对于堆栈溢出来说是离题的,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,请描述问题以及迄今为止为解决问题所做的工作

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

    • [1]
      以编程方式编辑或禁用标题颜色
    • [2]
      以编程方式编辑或禁用图标颜色
    • [3]
      在对话框中添加“确定”按钮
    镜像:,,

    必须向
    多图标列表首选项类添加什么?我找不到一本一般性考试的指南。。这就是为什么问得不专业的原因


    我卡住的地方:
    //R.styleable.iconReference+
    //R.styleable.IconPreference\u icon1+
    //R.styleable.IconPreference\u entrycons1(res/values/attrs.xml)
    //res/layout/list\u item\u dialog\u pref.xml
    //res/layout/preference_icon.xml
    //res/xml/preference.xml
    //com.android.app.MultiIconListPreference.java
    公共类MultiIconListPreference扩展了ListPreference{
    私人可牵引话筒;
    私有IconListPreferenceScreenAdapter iconListPreferenceAdapter=null;
    私有上下文;
    私人停车场;
    私有字符序列[]项;
    私有CharSequence[]入口值;
    private int[]mEntryIcons=null;
    私人共享参考优先权;
    私有共享参考。编辑器;
    私有字符串mKey;
    private int selectedEntry=-1;
    公共多图标列表首选项(上下文、属性集属性){
    这(上下文,属性,0);
    }
    公共多图标列表首选项(上下文上下文、属性集属性、int-defStyle){
    超级(上下文,attrs);
    setLayoutResource(R.layout.preference_图标);
    mContext=上下文;
    TypedArray a=上下文。获取样式属性(attrs,R.styleable.iconReference,defStyle,0);
    mIcon=a.getDrawable(R.styleable.iconReference\u icon1);
    int entryconsrisd=a.getResourceId(R.styleable.IconPreference\u entrycons1,-1);
    如果(EntryConsResid!=-1){
    SetEntryCons(EntryConsResid);
    }
    mInflater=LayoutInflater.from(上下文);
    mKey=getKey();
    prefs=PreferenceManager.getDefaultSharedReferences(getContext());
    editor=prefs.edit();
    a、 回收();
    }
    @凌驾
    公共字符串getValue(){
    如果(选择尝试!=-1)
    返回entryValues[selectedEntry].toString();
    返回super.getValue();
    }
    @凌驾
    公共无效onBindView(视图){
    super.onBindView(视图);
    ImageView=(ImageView)view.findViewById(R.id.icon);
    if(imageView!=null&&mIcon!=null){
    imageView.setImageDrawable(mIcon);
    }
    }
    公共无效设置图标(可绘制图标){
    如果((icon==null&&mIcon!=null)| |(icon!=null&&icon.equals(mIcon))){
    mIcon=图标;
    notifyChanged();
    }
    }
    公共可绘制getIcon(){
    返回mIcon;
    }
    公共无效设置入口图标(int[]入口图标){
    mentrycons=entrycons;
    }
    公共无效设置EntryCons(int EntryConsResid){
    TypedArray icons_array=mContext.getResources().obtainTypedArray(EntryConsResid);
    int[]icon_id_array=新的int[icons_array.length();
    对于(int i=0;i// 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;
            }
     
        }
    }