自定义布局的ListPreference

3
请告诉我在哪里可以阅读有关创建自定义布局列表首选项(背景和布局顶部面板,面板按钮)的内容。目前只能找到自定义行的示例。抱歉,以上内容由谷歌翻译器生成。
3个回答

5

我知道这是一篇旧帖子,但由于我现在正在编写我的自定义ListPrefence,所以我想为未来的程序员留下这个评论。我认为不能扩展ListPreference很糟糕,因为它违背了面向对象的方法为了实现我想要的功能,我实际上复制了90%的ListPreference代码。当我开始编写我的自定义ListPreference时,我希望只能添加我需要的10%。 - ilomambo
@ilomambo,更准确地说,ListPreference正在被DialogPreference所取代。 - tpbapp

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>

那么你的类应该是这样的:
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);
            }
        }
    }
    
    }
    

非常简单,谢谢。 - Ridcully

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接