Книга: Beginning Android

The Kind of Pop-Ups You Like

The Kind of Pop-Ups You Like

Of course, not all preferences are checkboxes and ringtones.

For others, like entry fields and lists, Android uses pop-up dialogs. Users do not enter their preference directly in the preference UI activity, but rather tap on a preference, fill in a value, and click OK to commit the change.

Structurally, in the preference XML, fields and lists are not significantly different from other preference types, as seen in this preference XML from the Prefs/Dialogs sample project available at http://apress.com:

<PreferenceScreen
 xmlns:android="http://schemas.android.com/apk/res/android">
 <PreferenceCategory android:title="Simple Preferences">
  <CheckBoxPreference
   android:key="@string/checkbox"
   android:title="Checkbox Preference"
   android:summary="Check it on, check it off"
  />
  <RingtonePreference
   android:key="@string/ringtone"
   android:title="Ringtone Preference"
   android:showDefault="true"
   android:showSilent="true"
   android:summary="Pick a tone, any tone"
  />
 </PreferenceCategory>
 <PreferenceCategory android:title="Detail Screens">
  <PreferenceScreen
   android:key="detail"
   android:title="Detail Screen"
   android:summary="Additional preferences held in another page">
   <CheckBoxPreference
    android:key="@string/checkbox2"
    android:title="Another Checkbox"
    android:summary="On. Off. It really doesn't matter."
   />
  </PreferenceScreen>
 </PreferenceCategory>
 <PreferenceCategory android:title="Simple Preferences">
  <EditTextPreference
   android:key="@string/text"
   android:title="Text Entry Dialog"
   android:summary="Click to pop up a field for entry"
   android:dialogTitle="Enter something useful"
  />
  <ListPreference
   android:key="@string/list"
   android:title="Selection Dialog"
   android:summary="Click to pop up a list to choose from"
   android:entries="@array/cities"
   android:entryValues="@array/airport_codes"
   android:dialogTitle="Choose a Pennsylvania city" />
 </PreferenceCategory>
</PreferenceScreen>

With the field (EditTextPreference), in addition to the title and summary you put on the preference itself, you can also supply the title to use for the dialog.

With the list (ListPreference), you supply both a dialog title and two string-array resources: one for the display names, one for the values. These need to be in the same order — the index of the chosen display name determines which value is stored as the preference in the SharedPreferences. For example, here are the arrays for use by the ListPreference shown previously:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string-array name="cities">
  <item>Philadelphia</item>
  <item>Pittsburgh</item>
  <item>Allentown/Bethlehem</item>
  <item>Erie</item>
  <item>Reading</item>
  <item>Scranton</item>
  <item>Lancaster</item>
  <item>Altoona</item>
  <item>Harrisburg</item>
 </string-array>
 <string-array name="airport_codes">
  <item>PHL</item>
  <item>PIT</item>
  <item>ABE</item>
  <item>ERI</item>
  <item>RDG</item>
  <item>AVP</item>
  <item>LNS</item>
  <item>AOO</item>
  <item>MDT</item>
 </string-array>
</resources>

When you bring up the preference UI, you start with another category with another pair of preference entries (see Figure 17-6).


Figure 17-6. The preference screen of the Dialogs project’s preference UI

Tapping the Text Entry Dialog preference brings up… a text-entry dialog — in this case, with the prior preference entry pre–filled in (Figure 17-7).


Figure 17-7. Editing a text preference

Tapping the Selection Dialog preference brings up… a selection dialog, showing the display names (Figure 17-8).


Figure 17-8. Editing a list preference

Оглавление книги


Генерация: 0.033. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз