2012-02-09 132 views
2

請在可以閱讀有關創建自定義佈局列表首選項(背景和佈局頂部面板,面板按鈕)的地方閱讀提示。遇見 - 僅用於自定義行的示例。 對不起 - 谷歌翻譯。自定義佈局ListPreference

回答

4

您無法爲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

4

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
  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