代替繁琐的finViewById
@EActivitypublic class MyActivity extends Activity { // Injects R.id.myEditText @ViewById EditText myEditText; @ViewById(R.id.myTextView) TextView textView;}
@EActivity(R.layout.main)public class MyActivity extends Activity { @ViewById TextView myTextView; @AfterViews void updateTextWithDate() { myTextView.setText("Date: " + new Date()); }}
注意:不要在onCreate中写任何对view相关的方法
@Extra 用于传递的Intent
@EActivitypublic class MyActivity extends Activity { @Extra("myStringExtra") String myMessage; @Extra("myDateExtra") Date myDateExtraWithDefaultValue = new Date(); // The name of the extra will be "myMessage" @Extra String myMessage;}
@EActivitypublic class MyActivity extends Activity { @Extra("myStringExtra") String myMessage; @Override protected void onNewIntent(Intent intent) { setIntent(intent); }}
@SystemServiceno more Context.getSystemService()
@EActivitypublic class MyActivity extends Activity { @SystemService NotificationManager notificationManager;}
定义:
@SharedPrefpublic 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();
@DefaultRes(R.string.defaultPrefName) String resourceName(); @DefaultRes String defaultPrefAge();
}
@EActivitypublic class MyActivity extends Activity { @Pref MyPrefs_ myPrefs;}// Simple editmyPrefs.name().put("John");// Batch editmyPrefs.edit() .name() .put("John") .age() .put(42) .apply();// Preference clearing:myPrefs.clear();// Check if a value exists:boolean nameExists = myPrefs.name().exists();// Reading a valuelong lastUpdated = myPrefs.lastUpdated().get();// Reading a value and providing a fallback default valuelong now = System.currentTimeMillis();long lastUpdated = myPrefs.lastUpdated().getOr(now);