自定义布局ListPreference

时间:2012-02-09 20:17:56

标签: android listpreference

提示请尽可能阅读有关创建自定义布局列表首选项(背景和布局顶部面板,面板按钮)的信息。 Met - 仅用于自定义行的示例。 对不起 - 谷歌翻译。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:4)

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

答案 2 :(得分:1)

  1. 在您的首选项xml文件

    &LT; your.domain.CustomListPreference ... /&GT;

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