简体   繁体   中英

How to create a DialogPreference programatically?

The background of my question is that I'd like to create a menu just like the "Wifi Networks" settings on my Samsung Galaxy S9000. The menu shows a list of known networks and has one additional list entry with the title "Add new network". If you click on it, fill out the dialog and hit "Ok" you get a new list item. The "Add new network" item is still there after this procedure.

My approach is to have a PreferenceScreen with one AdressDialogPreference which is a subclass of DialogPreference . The title of this preference is set to "Add new IP Address". When the user opens the dialog and enters values, the title of the AddressDialogPreference changes from "Add new IP Address" to the value the user entered in the dialog, eg "192.168.1.1".

But now I need a new AddressDialogPreference with the title "Add new IP Address", because the user may want to add more ip addresses. That is where I'm stuck. I tried the following:

@Override
protected void onDialogClosed(boolean positiveResult) {

    if(!positiveResult)
        return;

    (...)

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

But I have no idea where I can get/create an appropriate AttributeSet (the attr parameter). I cannot write a constructor without the AttributeSet because the base class DialogPreference requires an AttributeSet .

I tried to create an AttributeSet via Xml.asAttributeSet(XmlPullParser) but I have no clue what to feed to the XmlPullParser . The attributeCount was always -1 which is obviously wrong. The whole approach via a parser seems to me way to complicated.

The target API level is 8.

Excerpt from 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>

I'm not sure if this is relevant but this is how I loaded an AttributeSet into a SeekBarPreference (from http://android.hlidskialf.com/blog/code/android-seekbar-preference ), which is also derived from the DialogPreference class.

In your 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;
}

And this is the "res/layouts/seekbarpreference_layout.xml" file:

    <?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"
        />

If it's not working for you, perhaps you are missing a required attribute in the XML file?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM