请在可以阅读有关创建自定义布局列表首选项(背景和布局顶部面板,面板按钮)的地方提示。Met - 仅用于自定义行的示例。对不起-谷歌翻译。
问问题
10387 次
3 回答
4
您不能为ListPreference
. 但是,您可以创建自己的自定义DialogPreference
并将其设置为您想要的任何外观。
例如,这里的 aDialogPreference
使用 aTimePicker
允许用户选择时间。这是一个DialogPreference
允许用户选择颜色的。
于 2012-02-09T21:36:49.410 回答
3
在您的preference.xml文件中,您可以通过类的全名即com.example.MyPreference引用您的自定义ListPreference
<?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();
}
}
于 2012-12-25T18:35:11.650 回答
1
在您的偏好 xml 文件中
<your.domain.CustomListPreference .../>
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); } } } }
于 2013-09-03T20:43:06.637 回答