1. Trang chủ
  2. » Công Nghệ Thông Tin

Lập trình Androi part 33 ppsx

6 267 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 6
Dung lượng 271,85 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

If you nest PreferenceScreens, the parent screen displays the screen as a placeholder entry, and tapping that entry brings up the child screen.. For example, from the Prefs/Structured sa

Trang 1

Figure 21–3 The Simple project's list of saved preferences

Adding a Wee Bit o' Structure

If you have a lot of preferences for users to set, putting them all in one big list may not

be the best idea Android’s preference framework gives you a few ways to impose a bit

of structure on your bag of preferences, including categories and screens

Categories are added via a PreferenceCategory element in your preference XML and are

used to group together related preferences Rather than have your preferences all as

children of the root PreferenceScreen, you can place a few PreferenceCategory

elements in the PreferenceScreen, and then put your preferences in their appropriate

categories Visually, this adds a divider with the category title between groups of

preferences

If you have a whole lot of preferences—more than are convenient for users to scroll

through—you can also put them on separate “screens” by introducing the

PreferenceScreen element Yes, that PreferenceScreen element

Any children of PreferenceScreen go on their own screen If you nest

PreferenceScreens, the parent screen displays the screen as a placeholder entry, and

tapping that entry brings up the child screen

For example, from the Prefs/Structured sample project, here is a preference XML file

that contains both PreferenceCategory and nested PreferenceScreen elements:

<PreferenceScreen

xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="Simple Preferences">

<CheckBoxPreference

android:key="checkbox"

Trang 2

android:key="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="checkbox2"

android:title="Another Checkbox"

android:summary="On Off It really doesn't matter."

/>

</PreferenceScreen>

</PreferenceCategory>

</PreferenceScreen>

The result, when you use this preference XML with your PreferenceActivity implementation, is a categorized list of elements, as shown in Figure 21–4

Figure 21–4 The Structured project's preference UI, showing categories and a screen placeholder

If you tap the Detail Screen entry, you are taken to the child preference screen, as shown in Figure 21–5

Trang 3

Figure 21–5 The child preference screen of the Structured project's preference UI

The Kind of Pop-Ups You Like

Of course, not all preferences are check boxes 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 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:

<PreferenceScreen

xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="Simple Preferences">

<CheckBoxPreference

android:key="checkbox"

android:title="Checkbox Preference"

android:summary="Check it on, check it off"

/>

<RingtonePreference

android:key="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"

Trang 4

android:title="Another Checkbox"

android:summary="On Off It really doesn't matter."

/>

</PreferenceScreen>

</PreferenceCategory>

<PreferenceCategory android:title="Simple Preferences">

<EditTextPreference

android:key="text"

android:title="Text Entry Dialog"

android:summary="Click to pop up a field for entry"

android:dialogTitle="Enter something useful"

/>

<ListPreference

android:key="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 and one for the values These need to be in the same order, because 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 in the preceding example:

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

Trang 5

<item>MDT</item>

</string-array>

</resources>

When you bring up the preference UI, you start with another category with another pair

of preference entries, as shown in Figure 21–6

Figure 21–6 The preference screen of the Dialogs project's preference UI

Tapping the Text Entry Dialog entry brings up a text-entry dialog with the prior

preference entry already filled in, as shown in Figure 21–7

Figure 21–7 Editing a text preference

Trang 6

Figure 21–8 Editing a list preference

Ngày đăng: 01/07/2014, 21:20