简体   繁体   中英

Best way to implement a complex preference screen?

For my app, I have a fairly complex set of configuration options that user can pick from. I'm currently using a PreferenceActivity as my user interface for these options and the options are stored using shared preferences. As an example of some of the settings I have to accommodate:

  • Lists of pairs: to select a background pattern, the user can select to use from 1 to 5 different shapes (where each shape is a .png file) and assign each shape an int color. For example, the user could pick an orange square, a green triangle and a red rectangle.

  • Hierarchical data: one part of my app can be configured to use one of five modes. Each mode has a few associated unique settings eg one mode requires two integers to be chosen the other mode might require one boolean to be chosen.

However, my feeling is PreferenceActivity doesn't work well with settings like the above because:

  • Shared preferences cannot store lists.
  • Shared preferences cannot store hierarchical data.
  • Boiling my preference interface down to eg individual preference buttons for configuring each color and using dependent preferences to disable preferences that aren't applicable with the current mode is going to make for a messy and hard to use interface.

I could write my own Preference classes for configuring the lists, but I find these really laborious to implement compared to implementing a typical View and I've still got to deal with the storage issues.

My plan was:

  • Just implement a custom activity with a custom GUI. This gives me much more freedom for doing a nice interface for configuring the lists and I can do intelligent hiding of options that aren't applicable to the current mode.
  • Store all my settings in either an XML file or by serialising a Java object. This means I can easily support hierarchical data and variable length lists, which allows room for further extensions.

I'm concerned I'm not doing things the Android way, but it seems to me that shared preferences and PreferenceActivity aren't suited to my needs here.

Since I began in Android, I have always created my own preference activities. It seems really difficult because there is not much documentation on the internet on how to do it, but it is, in fact, really quite simple. As you said, it gives you a lot more freedom in deciding exactly how your UI looks and acts. Just in case you're wondering how to do preferences yourself, here's a little simple snippet:

public class myprefs extends Activity{
private static final String PREFS_XML = "prefs_xml";
private static final String PREF_1 = "pref_1";

String preference;

private SharedPreferences preferences = null;
public void loadPrefs(){
    preferences = this.getSharedPreferences(PREFS_XML, Activity.MODE_PRIVATE);
    preference = preferences.getString(PREF_1, "default value");
}
}

That's pretty much how simple it is to get your own preferences. To set them you use

preferences.edit().putString(PREF_1, "hello!").commit();

That can be put into an onClick, onItemSelected, or any other 'event' you want to put it into. I made 'preferences' a class wide instance so that I can access it anywhere in the class without having to re-instantiate it. I hope this helps you out a little bit. As a specific answer to your specific question, I would think your plan is perfectly reasonable.

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