编写新的 DialogPreference 类的简洁方法? [英] Concise way of writing new DialogPreference classes?

查看:10
本文介绍了编写新的 DialogPreference 类的简洁方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过扩展 DialogPreference 类在 Android 中编写一些自定义首选项对话框.但是,我有点担心为此所需的样板代码数量,因为似乎有很多行为需要测试.

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.

例如,这个数字偏好对话框的例子是相当典型的:http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/src/net/jimblackler/newswidget/NumberPreference.java

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

特别是 onSave()/RestoreInstanceState() 方法和class SavedState"部分,它们是必需的,以便对对话框的当前更改保留在方向更改非常冗长和复杂.

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.

有没有人有任何以更简洁的方式编写 DialogPreference 类的技巧?

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

推荐答案

最低限度是:

  1. MyCustomDialogPreference(Context context, AttributeSet attrs) 构造函数.
    • 不要忘记调用super(context, attrs).
    • 调用 setPersistent(false) 以向超级 Preference 类指示您自己保留首选项值.
    • 如果您想从资源中扩展对话框面板布局,还可以调用 setDialogLayoutResource(int dialogLayoutResId).
  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).
  • 不要忘记调用super.onBindDialogView(view).
  • 不要忘记调用super.onDialogClosed(positiveResult).

这是最低限度,它假设:

This was the bare minimum, and it assumes that:

  • 您的自定义 DialogPreference 管理单个首选项键/值对.
  • 您负责保留偏好值.
  • 您正在从资源中扩展对话框面板布局.

(a) 如果你想以编程方式创建对话框面板布局,那么也实现 onCreateDialogView() 而不是调用 setDialogLayoutResource()在构造函数中.

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

(b) 如果您的首选项仅支持单个键/值对,那么您可以使用辅助保存方法 persistBoolean(boolean)、persistFloat(float)、persistInt(int)、persistLong(long)、persistString(String) 当您在 onDialogClosed() 中保留更改的首选项值时.否则,您需要使用 getEditor() 方法,如下所示:

(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) 如果您打算从膨胀的 xml 中提供默认值,那么您还需要实现 onGetDefaultValue(TypedArray a, int index) 方法.

(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,我知道你问这个问题已经一个月了.我希望你仍然可以使用它.

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

这篇关于编写新的 DialogPreference 类的简洁方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆