AndroidAnnotations注解框架之@Pref(二)

AndroidAnnotations是一个能够让你快速进行Android开发的开源框架,它能让你专注于真正重要的地方。使代码更加精简,使项目更加容易维护,它的目标就是“Fast Android Development.Easy maintainance”.
 

 

希望此篇能帮到大家,本系列教程目录:转载请链接并注明:转自 小树技术博客 .

AndroidAnnotations注解框架之介绍+配置(一) (飞机票)
 

 

 

Anno 提供了一种类型安全的方式,让你可以去调用Android SharedPreferences , 下面带来一些Preferences常用的操作,并在最后附上一个项目的Demo片段

 

1.定义Preferences

 

首先,你应该创建一个以@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 0
long lastUpdated();

}
基于这个操作, AndroidAnnotations框架将会自动构建一个SharedPreferences子类,在你的原类名基础上加上下划线,如MyPref你可以通过@Pref注释得到这个实例

注意: 实例的类型必须以新生成的类为类型

@EActivity
public class MyActivity extends Activity {

@Pref
MyPrefs_ myPrefs;

// ...

}
其中,定义值目前有:

@DefaultBoolean

@DefaultFloat

@DefaultInt

@DefaultLong

@DefaultString

@DefaultStringSet

@DefaultRes
 

 

2.使用pref

 

// 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);
 

3.设置默认值

 

注意: anno 3.0 版本之后才有这个功能

@SharedPref
public interface MyPrefs {
@DefaultInt(0)
int versionCode();

@DefaultString("无")
String widgetBackground();

@DefaultBoolean(false)
boolean classTableAsFirstScreen();}</pre>

 

4.使用String 资源

 

注意:anno 3.1 后才有此功能

@SharedPref
public interface MyPrefs {
@DefaultString(value = “John”, keyRes = R.string.myPrefKey)
String name();

@DefaultRes
String defaultPrefAge(keyRes = R.string.myOtherPrefKey);

}
 

5.作用域

 

你还可以通过设置value来设定pref的作用域

例如:

  • ACTIVITY :名为:MyActivity_MyPrefs 的pref

  • ACTIVITY_DEFAULT 名为 MyActivity 的 pref
    ( 可以通过 activity.getPreferences() 获取到);

  • APPLICATION_DEFAULT <span style="font-family: Courier New;">:</span> 使用默认的单例名称
     

 

 

 

 

 

因此,如果一个SharedPreference接口的定义是给一个应用程序application下所有activity所使用的话,应该使用以下作用域:

@SharedPref(value=SharedPref.Scope.UNIQUE)
public interface MyPrefs {

 

6.在PreferenceActivity中使用它

 

同样地,使用 Android PreferenceActivity能够改写任何已用@SharedPref注解的SharedPref

@SharedPref()
public interface MyPrefs {

}

 

public class PrefsActivity extends PreferenceActivity {

public static String PREF_NAME = "MyPrefs";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

            // Using your MyPrefs values 
    this.getPreferenceManager().setSharedPreferencesName(PREF_NAME);

            // Opening the layout 
    addPreferencesFromResource(R.xml.prefs);
}

}
 

7.实际项目demo片段

 

SharedPref

**

  • @Title:应用程序配置文件AppConfig
  • @Description: 使用SharedPref储存本地数据
  • @author qsuron
  • @date 2014-9-23
  • @email admin@qiushurong.cn
    */
    @SharedPref( value = SharedPref.Scope.UNIQUE)
    public interface AppConfig {

    @DefaultInt(0)
    int versionCode(); // 当前的versionCode

    @DefaultString(“”)
    String userName(); // 用户

    @DefaultString(“”)
    String password(); // 密码
    }
    片段1

     config = new cn.scau.scautreasure.AppConfig_(context);

    RingerMode duringMode = RingerMode.getModeByValue(config.duringClassRingerMode().get());
    RingerMode afterMode = RingerMode.getModeByValue(config.afterClassRingerMode().get());
    


    片段2

    @Pref        cn.scau.scautreasure.AppConfig_  config;

    /**

    • 切换到加载所有课程模式;
      */
      @OptionsItem
      void menu_load_classtable_all(){
      config.classTableShowMode().put(MODE_ALL);
      showClassTable();
      }

      /**

    • 切换到智能加载课程模式;
      */
      @OptionsItem
      void menu_load_classtable_with_params(MenuItem item){
      config.classTableShowMode().put(MODE_PARAMS);
      showClassTable();
      }
       

 

Edit by 小树