自定义布局ListPreference

自定义布局ListPreference

问题描述:

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

您无法为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

+0

我知道这是一个古老的职位,但因为我我正在编写自定义的ListPrefence,现在我可能会为未来的编码人员留下此评论。 **我认为无法扩展ListPreference是不好的,因为它破坏了面向对象的方法**为了实现我想要的,我实际上复制了ListPreference代码的90%。当我开始编写自定义ListPreference时,我希望只能添加我需要的10%。 – ilomambo 2013-12-27 11:47:24

+0

@ilomambo更像是ListPreference正在被弃用,以支持DialogPreference – tpbapp 2014-03-28 13:23:21

preference.xml文件,你可以通过类,即全名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 HOULD的东西是指您的自定义ListPreference像这样:

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(); 
    } 
} 

  1. 在您的喜好xml文件

    < your.domain.CustomListPreference .../>

  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); 
         } 
        } 
    } 
    
    } 
    
+0

实际上很简单。非常感谢。 – Ridcully 2015-07-12 18:58:25