Книга: Beginning Android

And Now, a Word from Our Framework

And Now, a Word from Our Framework

Beginning with the 0.9 SDK, Android has a framework for managing preferences. This framework does not change anything mentioned previously. Instead, the framework is more for presenting consistent preference-setting options for users so different applications do not have to reinvent the wheel.

The linchpin to the preferences framework is yet another XML data structure. You can describe your application’s preferences in an XML file stored in your project’s res/xml/ directory. Given that, Android can present a pleasant UI for manipulating those preferences, which are then stored in the SharedPreferences you get back from getDefaultSharedPreferences().

The following is the preference XML for the Prefs/Simple preferences sample project available in the Source Code section at http://apress.com:

  android:title="Checkbox Preference"
  android:summary="Check it on, check it off" />
  android:title="Ringtone Preference"
  android:summary="Pick a tone, any tone" />

The root of the preference XML is a PreferenceScreen element. (I will explain why it is named that later in this chapter; for now, take it on faith that it is a sensible name.) One of the things you can have inside a PreferenceScreen element, not surprisingly, is preference definitions — subclasses of Preference, such as CheckBoxPreference or RingtonePreference, as shown in the preceding code. As one might expect, these allow you to check a checkbox and choose a ringtone, respectively. In the case of RingtonePreference, you have the option of allowing users to choose the system-default ringtone or to choose “silence” as a ringtone.

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

Генерация: 0.345. Запросов К БД/Cache: 3 / 1
Вверх Вниз