写新的对话框preference类的简洁的方式? [英] Concise way of writing new DialogPreference classes?

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

问题描述

我正在写在Android的一些自定义的preference对话框通过扩展对话preference 类。不过,我变得有点担心时所需要这个,因为似乎很多行为测试的锅炉板code量。

有关例如,若干preference对话框的该例子是相当典型的: <一href="http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/src/net/jimblackler/newswidget/Number$p$pference.java">http://svn.jimblackler.net/jimblackler/trunk/workspace/NewsWidget/src/net/jimblackler/newswidget/Number$p$pference.java

在特定的的OnSave() / RestoreInstanceState()方法和阶级SavedState部分,分别是必要的,这样的电流变化到对话框被保留在取向的变化是相当冗长和复杂的。

有没有人有任何提示编写对话preference类以更简洁的方式?

解决方案

的最低限度是:

  1. MyCustomDialog preference(上下文的背景下,ATTRS的AttributeSet) 的构造。
    • 请不要忘记调用超(背景下,ATTRS)
    • 呼叫 setPersistent(假)来指示,你坚持你自己的preference价值超preference类。
    • 如果您想从资源膨胀对话框面板布局,然后还要求 setDialogLayoutResource(INT dialogLayoutResId)
  2. onBindDialogView(查看视图) - 更新与您的preference值(S)的观点。
    • 请不要忘记调用 super.onBindDialogView(视图)
  3. onDialogClosed(布尔positiveResult) - 如果positiveResult为真,那么坚持从您的视角以共享preferences值(S) 。
    • 请不要忘记调用 super.onDialogClosed(positiveResult)

这是最低限度,并假定:

  • 您的自定义对话框preference管理一个preference键/值对。
  • 您有责任坚持了preference值。
  • 您正在膨胀从资源对话框面板布局。

现在的一些附加选项:

(一)如果你想通过程序生成的对话框面板布局,然后也实现 onCreateDialogView() 调用而不是 setDialogLayoutResource()在构造函数中。

(二)如果你的preference只支持一个键/值对,那么你可以使用助手保存方法 persistBoolean(布尔),persistFloat(浮点),persistInt(INT), persistLong(长),persistString(字符串)当你坚持onDialogClosed改变preference值()。否则,你需要使用 getEditor()的方法,像这样:

 私人MyCustomView MyView的;

@覆盖
保护无效onBindDialogView(查看视图){
    super.onBindDialogView(视图);

    //该观点得到了我的自定义创建onCreateDialogView()
    MyView的=(MyCustomView)视图;

    共享preferences共享preferences = getShared preferences();
    myView.setValue1(共享preferences.getString(myKey1,myDefaultValue1));
    myView.setValue2(共享preferences.getString(myKey2,myDefaultValue2));
}

@覆盖
保护无效onDialogClosed(布尔positiveResult){
    super.onDialogClosed(positiveResult);

    如果(positiveResult){
        编辑EDITOR = getEditor();
        editor.putString(myKey1,myView.getValue1());
        editor.putString(myKey2,myView.getValue2());
        editor.commit();
    }
}
 

(三)如果你计划从充气XML提供的默认值,那么你还需要落实 onGetDefaultValue(TypedArray一个,INT指数)方法。


@RichardNewton,我知道,一个月过去了,因为你问的问题。我希望你仍然可以使用它。

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?

解决方案

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.

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

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