Java – the best way to implement a complex preferences screen?

For my application, I have a fairly complex set of configuration options for users to choose from I am currently using preferenceactivity as the user interface for these options and using the shared preference storage option As an example of some settings I have to adapt to:

>Pair list: to select the background pattern, the user can choose to use 1 to 5 different shapes (each shape is a. PNG file) and specify an int color for each shape For example, the user can select orange squares, green triangles and red rectangles. > Layered data: part of my application can be configured to use one of five modes Each mode has some unique settings. For example, one mode needs to select two integers, and the other mode may need to select a Boolean value

However, I feel that the preferenceactivity is incompatible with the above settings because:

>Shared preferences cannot store list. > Sharing preferences cannot store tiered data. > Simplifying my preferences interface to, for example, individual preference buttons for configuring each color and using dependent preferences to disable preferences that do not apply to the current mode will lead to a cluttered and difficult interface

I can write my own preference class to configure the list, but I find it very laborious to implement them compared with the typical view, and I still need to deal with the storage problem

My plan is:

>Just use the custom GUI to implement custom activities This gives me more freedom to make a beautiful interface for the configuration list, and I can intelligently hide options that are not applicable to the current mode. > Store all my settings in an XML file or by serializing Java objects This means that I can easily support hierarchical data and variable length lists, which provides space for further expansion

Does the plan look reasonable? I'm worried that I'm not doing things Android way, but in my opinion, sharing preferences and preferenceactivity are not suitable for my needs

Solution

Since I started using Android, I have always created my own preference activities It seems really difficult because there are not many documents on how to do it on the Internet, but in fact it is very simple As you said, it gives you more freedom to determine the appearance and behavior of the UI If you want to know how to make your own preferences, here is a simple clip:

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");
}
}

It's easy to get your preferences To set them, use

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

This can be placed in onclick, onitemselected, or any other "event" you want to place I set 'preferences' as a class scoped instance so that I can access it anywhere in the class without re instantiating it I hope this will help you a little As the specific answer to your specific question, I think your plan is very reasonable

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>