【问题标题】:Custom layout ListPreference自定义布局 ListPreference
【发布时间】:2012-02-09 20:17:55
【问题描述】:

请在可以阅读有关创建自定义布局列表首选项(背景和布局顶部面板,面板按钮)的地方提示。 Met - 仅用于自定义行的示例。 抱歉 - 谷歌翻译。

【问题讨论】:

    标签: android listpreference


    【解决方案1】:

    您不能为ListPreference 创建自定义布局。但是,您可以创建自己的自定义 DialogPreference 并将其设置为您希望的样子。

    例如,here is a DialogPreference that uses a TimePicker to allow the user to choose a timeHere is a DialogPreference that allows the user to choose a color.

    【讨论】:

    • 我知道这是一篇旧帖子,但由于我现在正在编写自定义的 ListPrefence 代码,因此我可能会将这条评论留给未来的程序员。 **我认为不能扩展 ListPreference 很糟糕,因为它违背了面向对象的方法** 为了实现我想要的,我实际上复制了 90% 的 ListPreference 代码。当我开始编写我的自定义 ListPreference 时,我希望我只能添加我需要的 10%。
    • @ilomambo 它更像是 ListPreference 被弃用,取而代之的是 DialogPreference
    【解决方案2】:

    在您的 preference.xml 文件中,您可以通过类的全名来引用您的自定义 ListPreference,即 com.example.MyPreference

    <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
        android:key="pref_wifi_key"
        android:title="@string/settings">
        <ListPreference
            android:key="pref_wifi_remove"
            android:title="@string/remove_wifi"/>
        <com.example.MyPreference
        android:title="@string/add_wifi"/>
    </PreferenceScreen>
    

    那么你的类 MyPreference 应该是这样的:

    import android.preference.ListPreference;
    
    public class MyPreference extends ListPreference  {
    
        Context context;
    
        public MyPreference(Context context, AttributeSet attrs) {
            this.context = context;
            setDialogLayoutResource(R.layout.yourLayout); //inherited from DialogPreference
            setEntries(new CharSequence[] {"one", "two"});
            setEntryValues(new CharSequence[] {"item1", "item2"});
        }
        protected void onDialogClosed(boolean positiveResult) {
        Toast.makeText(context, "item_selected",
                           Toast.LENGTH_SHORT).show();
        }
    }
    

    【讨论】:

      【解决方案3】:
      1. 在您的偏好 xml 文件中

      2. CustomListPreference.java

        class CustomListPreference extends ListPreference {
        
        mListAdapter = new your_custom_list_adapter();
        private int mClickedDialogEntryIndex;
        public CustomListPreference(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        
        public CustomListPreference(Context context) {
            super(context);
        }
        
        @Override
        protected void onPrepareDialogBuilder(Builder builder) {
              mClickedDialogEntryIndex = findIndexOfValue(getValue());
              builder.setSingleChoiceItems(mListAdapter, mClickedDialogEntryIndex,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            if (mClickedDialogEntryIndex != which) {
                                mClickedDialogEntryIndex = which;
                                CustomListPreference.this.notifyChanged();
                            }
                            CustomListPreference.this.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                            dialog.dismiss();
                        }
                    });
              builder.setPositiveButton(null, null);
        }
        
        @Override
        protected void onDialogClosed(boolean positiveResult) {
            CharSequence[] entryValues = getEntryValues();
            if (positiveResult && mClickedDialogEntryIndex >= 0 && entryValues != null) {
                String value = entryValues[mClickedDialogEntryIndex].toString();
                if (callChangeListener(value)) {
                    setValue(value);
                }
            }
        }
        
        }
        

      【讨论】:

      • 其实很简单。非常感谢。
      猜你喜欢
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2018-08-02
      • 2015-04-05
      • 2017-10-15
      • 2016-06-28
      • 2015-08-11
      相关资源
      最近更新 更多