And you will need to arrange to invoke the activity, such as from a menu option, here pulled from SimplePrefsDemo at http://apress.com:

@Override

public boolean onCreateOptionsMenu (Menu menu) {

 menu.add(Menu.NONE, EDIT_ID, Menu.NONE, 'Edit Prefs')

  .setIcon(R.drawable.misc)

  .setAlphabeticShortcut('e');

 menu.add(Menu.NONE, CLOSE_ID, Menu.NONE, 'Close')

  .setIcon(R.drawable.eject)

  .setAlphabeticShortcut('c');

 return(super.onCreateOptionsMenu (menu));

}

@Override

public boolean onOptionsItemSelected(MenuItem item) {

 switch (item.getItemId()) {

  case EDIT_ID:

   startActivity(new Intent(this, EditPreferences.class));

   return(true);

  case CLOSE_ID:

   finish();

   return(true);

 }

 return(super.onOptionsItemSelected (item));

}

However, that is all that is needed, and it really is not that much code outside of the preferences XML. What you get for your effort is an Android-supplied preference UI, as shown in Figure 17-1.

Figure 17-1. The Simple project’s preferences UI

The checkbox can be directly checked or unchecked. To change the ringtone preference, just click on the entry in the preference list to bring up a selection dialog like the one in Figure 17-2.

Figure 17-2. Choosing a ringtone preference

Note that there is no explicit Save or Commit button or menu — changes are persisted as soon as they are made.

The SimplePrefsDemo activity, beyond having the aforementioned menu, also displays the current preferences via a TableLayout:

<?xml version='1.0' encoding='utf-8'?>

<TableLayout

 xmlns:android='http://schemas.android.com/apk/res/android'

 android:layout_width='fill_parent'

 android:layout_height='fill_parent'

>

 <TableRow>

  <TextView

   android:text='Checkbox:'

   android:paddingRight='5px'

  />

  <TextView android:id='@+id/checkbox'

  />

 </TableRow>

 <TableRow>

  <TextView

   android:text='Ringtone:'

   android:paddingRight='5px'

  />

  <TextView android:id='@+id/ringtone'

  />

 </TableRow>

</TableLayout>

The fields for the table are found in onCreate():

@Override

public void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);

 setContentView(R.layout.main);

 checkbox = (TextView)findViewById(R.id.checkbox);

 ringtone = (TextView)findViewById(R.id.ringtone);

}

The fields are updated on each onResume():

@Override

public void onResume() {

 super.onResume();

 SharedPreferences prefs = PreferenceManager

  .getDefaultSharedPreferences(this);

 checkbox.setText(new Boolean(prefs

  .getBoolean('checkbox', false)).toString());

 ringtone.setText(prefs.getString('ringtone', '<unset>'));

}

This means the fields will be updated when the activity is opened and after the preferences activity is left (e.g., via the back button); see Figure 17-3.

Figure 17-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, having them all in one big list may become troublesome. Android’s preference framework gives you a few ways to impose a bit of structure on your bag of preferences,

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ОБРАНЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату