【问题标题】:How to create a DialogPreference programatically?如何以编程方式创建 DialogPreference?
【发布时间】:2011-04-24 19:24:30
【问题描述】:

我的问题的背景是我想创建一个菜单,就像我的三星 Galaxy S9000 上的“Wifi 网络”设置一样。该菜单显示了已知网络的列表,并有一个附加的列表条目,标题为“添加新网络”。如果单击它,填写对话框并单击“确定”,您将获得一个新的列表项。完成此过程后,“添加新网络”项仍然存在。

我的方法是有一个 PreferenceScreen 和一个 AdressDialogPreference,它是 DialogPreference 的子类。此首选项的标题设置为“添加新 IP 地址”。当用户打开对话框并输入值时,AddressDialogPreference 的标题从“添加新 IP 地址”更改为用户在对话框中输入的值,例如“192.168.1.1”。

但现在我需要一个标题为“添加新 IP 地址”的新 AddressDialogPreference,因为用户可能想要添加更多 IP 地址。那就是我卡住的地方。我尝试了以下方法:

@Override
protected void onDialogClosed(boolean positiveResult) {

    if(!positiveResult)
        return;

    (...)

    PreferenceScreen ps = (PreferenceScreen)pm.findPreference("validIPs");      
    ps.addPreference(new AddressDialogPreference(getContext(), attr ));
}

但我不知道在哪里可以获得/创建适当的 AttributeSetattr 参数)。我无法编写没有 AttributeSet 的构造函数,因为基类 DialogPreference 需要 AttributeSet

我尝试通过 Xml.asAttributeSet(XmlPullParser) 创建一个 AttributeSet,但我不知道向 XmlPullParser 提供什么。 attributeCount 始终为 -1,这显然是错误的。在我看来,通过解析器的整个方法很复杂。

目标 API 级别为 8。

preferences.xml 节选:

<PreferenceScreen android:title="@string/validIPsHeadline"
android:summary="@string/validIPsSummary" 
android:dependency="restrictIPs"
android:key="validIPs"> 

<com.websliders.preferences.AddressDialogPreference
android:title="@string/defaultIPHeadline" 
android:summary="@string/defaultIPSummary" />
</PreferenceScreen>

【问题讨论】:

    标签: android android-preferences


    【解决方案1】:

    我不确定这是否相关,但这就是我将 AttributeSet 加载到 SeekBarPreference (来自 http://android.hlidskialf.com/blog/code/android-seekbar-preference )的方式,它也是从 DialogPreference 类派生的。

    在您的 PreferenceActivity 中:

    public PreferenceScreen getPreferenceScreen(String title, String summary) {
        // Root
        PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
    
        PreferenceCategory inlinePrefCat = new PreferenceCategory(this);
        inlinePrefCat.setTitle("DNA");
        root.addPreference(inlinePrefCat);
    
        //This is where you load the AttributeSet 
        //from the res/layouts/seekbarpreference_layout.xml file
        Preference editTextPref;
        Resources resources = this.getResources();
        XmlPullParser parser = resources.getXml(R.layout.seekbarpreference_layout);
        AttributeSet attributes = Xml.asAttributeSet(parser);
    
        editTextPref = new SeekBarPreference(this, attributes);
    
    
        editTextPref.setKey(title);
        editTextPref.setTitle(title);
        editTextPref.setSummary(summary);
        //editTextPref.setText(text)
        inlinePrefCat.addPreference(editTextPref);
    
        return root;
    }
    

    这是“res/layouts/seekbarpreference_layout.xml”文件:

        <?xml version="1.0" encoding="utf-8"?>
    <com.hlidskialf.android.preference.SeekBarPreference
            xmlns:android="http://schemas.android.com/apk/res/android"
             android:key="duration"
            android:title="Duration of something"
            android:summary="How long something will last"
            android:dialogMessage="Something duration"
            android:defaultValue="5"
            android:text=" minutes"
            android:max="60"
            />
    

    如果它不适合您,也许您在 XML 文件中缺少必需的属性?

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 2013-11-15
      • 2013-12-28
      • 2011-02-10
      • 2011-04-02
      • 2012-02-26
      • 2011-03-01
      • 2014-06-18
      • 1970-01-01
      相关资源
      最近更新 更多