Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

java - Concise way of writing new DialogPreference classes?

I'm writing some custom preference dialogs in Android by extending the DialogPreference class. However, I'm getting a bit concerned at the amount of boiler plate code that is needed for this as there appears to be a lot of behaviour to test.

For instance, this example of a number preference dialog is fairly typical: http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/src/net/jimblackler/newswidget/NumberPreference.java

In particular, the onSave()/RestoreInstanceState() methods and "class SavedState" parts, which are needed so that the current changes to the dialog are retained on orientation changes are quite verbose and complex.

Does anyone have any tips for writing DialogPreference classes in a more concise way?

question from:https://stackoverflow.com/questions/4505845/concise-way-of-writing-new-dialogpreference-classes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The bare minimum is:

  1. MyCustomDialogPreference(Context context, AttributeSet attrs) constructor.
    • Don't forget to call super(context, attrs).
    • Call setPersistent(false) to indicate to the super Preference class that you persist the preference value on your own.
    • If you want to inflate the dialog panel layout from a resource, then also call setDialogLayoutResource(int dialogLayoutResId).
  2. onBindDialogView(View view) - update the view with the value(s) of your preference.
    • Don't forget to call super.onBindDialogView(view).
  3. onDialogClosed(boolean positiveResult) - if positiveResult is true then persist the value(s) from your view to the SharedPreferences.
    • Don't forget to call super.onDialogClosed(positiveResult).

This was the bare minimum, and it assumes that:

  • Your custom DialogPreference manages a single preference key/value pair.
  • You are responsible for persisting the preference value.
  • You are inflating the dialog panel layout from a resource.

Now for some additional options:

(a) If you want to create the dialog panel layout programmatically, then implement also onCreateDialogView() instead of calling setDialogLayoutResource() in the constructor.

(b) If your preference supports only a single key/value pair, then you can use the helper save methods persistBoolean(boolean), persistFloat(float), persistInt(int), persistLong(long), persistString(String) when you persist the changed preference value in onDialogClosed(). Otherwise, you need to use the getEditor() method, like so:

private MyCustomView myView;

@Override
protected void onBindDialogView(View view) {
    super.onBindDialogView(view);

    // the view was created by my custom onCreateDialogView()
    myView = (MyCustomView)view;

    SharedPreferences sharedPreferences = getSharedPreferences();
    myView.setValue1(sharedPreferences.getString(myKey1, myDefaultValue1));
    myView.setValue2(sharedPreferences.getString(myKey2, myDefaultValue2));
}

@Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (positiveResult) {
        Editor editor = getEditor();
        editor.putString(myKey1, myView.getValue1());
        editor.putString(myKey2, myView.getValue2());
        editor.commit();
    }
}

(c) If you plan to supply default values from an inflated xml, then you need to implement also the onGetDefaultValue(TypedArray a, int index) method.


@RichardNewton, I know that a month has passed since you asked the question. I hope you can still use it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...