簡體   English   中英

如何以編程方式創建DialogPreference?

[英]How to create a DialogPreference programatically?

我的問題的背景是,我想創建一個菜單,就像在三星Galaxy S9000上的“ Wifi網絡”設置一樣。 該菜單顯示了已知網絡的列表,並帶有一個標題為“添加新網絡”的附加列表條目。 如果單擊它,請填寫對話框並單擊“確定”,您將獲得一個新的列表項。 此過程之后,“添加新網絡”項仍然存在。

我的方法是創建一個帶有一個AdressDialogPreferencePreferenceScreen ,它是DialogPreference的子類。 此首選項的標題設置為“添加新IP地址”。 當用戶打開對話框並輸入值時, AddressDialogPreference的標題將從“添加新IP地址”更改為用戶在對話框中輸入的值,例如“ 192.168.1.1”。

但是現在我需要一個標題為“ Add new IP Address”的新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>

我不確定這是否相關,但這是我將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文件中缺少必需的屬性?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM