当前位置: 代码迷 >> 综合 >> AndroidAnnotations——SharedPreferencesHelpers
  详细解决方案

AndroidAnnotations——SharedPreferencesHelpers

热度:80   发布时间:2023-12-11 23:57:47.0
  139人阅读  评论(4)  收藏  举报
AndroidAnnotation

目录(?)[+]

SharedPreferencesHelpers

Since AndroidAnnotations 2.1


SharedPreferences helpers allow you to use Android  SharedPreferences, but in a typesafe manner, instead of using strings.
SharedPreferences辅助可以让你使用Android   SharedPreferences ,但是用了类型安全方式来代替strings。

Defining the preferences


First, you should create an interface annotated with  @SharedPref to define the SharedPreferences :
首先,你应该生成一个加了   @SharedPref 注解的接口来定义SharedPreferences:

@SharedPref
public interface MyPrefs {
        // The field name will have default value "John"@DefaultString("John")String name();// The field age will have default value 42@DefaultInt(42)int age();// The field lastUpdated will have default value 0long lastUpdated();}

Based on that specification, AndroidAnnotations builds a SharedPreferences Helper that has the same name plus an underscore. You can get an instance of the generated helper in any enhanced class with the  @Pref annotation.
基于上述说明,AA会创建一个 SharedPreferences 辅助,它的文件名字和父类相同,只是附加一个下划线。你可以使用   @Pref   注解在任意优化的类中获取生成的辅助的实例。

Important: The type of the field  MUST be the generated class instead of the source class. It's the only exception in AA.
重要提示:字段类型 必须是生成类。这是AA中唯一的特例。
@EActivity
public class MyActivity extends Activity {
        @PrefMyPrefs_ myPrefs;// ...}

You can then start using it:
然后你就可以开始使用了:

// Simple edit myPrefs.name().put("John");// Batch edit myPrefs.edit().name().put("John").age().put(42).apply();// Preference clearing: myPrefs.clear();// Check if a value exists: boolean nameExists = myPrefs.name().exists();// Reading a value long lastUpdated = myPrefs.lastUpdated().get();// Reading a value and providing a fallback default value long now = System.currentTimeMillis();
long lastUpdated = myPrefs.lastUpdated().getOr(now);

Default resource value

Since AndroidAnnotations 3.0


It's now possible to inject a default value from Android resources with  @DefaultRes:
目前可以使用   @DefaultRes 注解从Android资源注入默认值:

@SharedPref
public interface MyPrefs {
        @DefaultRes(R.string.defaultPrefName)String resourceName();@DefaultResString defaultPrefAge();
}

Scope


Observe that you can name the shared preference by setting  value to one of the following:
需要注意的是你可以设置   value 为以下一项来命名 shared preference
  • ACTIVITY, for a shared preference named MyActivity_MyPrefs;
  • ACTIVITY, 则shared preference 命名为MyActivity_MyPrefs;
  • ACTIVITY_DEFAULT, for a shared preference named MyActivity (also available throughactivity.getPreferences());
  • ACTIVITY_DEFAULT, 则shared preference 命名为MyActivity (也可以通过activity.getPreferences()获取);
  • APPLICATION_DEFAULT, for the default SharedPreference or UNIQUE, for a shared preference named MyPrefs.
  • APPLICATION_DEFAULT, 默认为 SharedPreference 或者 UNIQUE, 则shared preference 命名为MyPrefs.

Therefore, if a single shared preference is needed for the interface defined,  in order to all activities of a given application to share the same preferences, the following should be used:
因此,假如需要一个单例shared preference,为了给定application的所有activity可以共享数据,可以使用以下方法:
@SharedPref(value=SharedPref.Scope.UNIQUE)
public interface MyPrefs {
...

本文档的简单示例下载

  相关解决方案