代替繁琐的finViewById
@EActivity public class MyActivity extends Activity { // Injects R.id.myEditText @ViewById EditText myEditText; @ViewById(R.id.myTextView) TextView textView; }
指定需要在视图加载完成后才能执行的方法@AfterViews
@EActivity(R.layout.main) public class MyActivity extends Activity { @ViewById TextView myTextView; @AfterViews void updateTextWithDate() { myTextView.setText("Date: " + new Date()); } }
注意:不要在onCreate中写任何对view相关的方法
@Extra 用于传递的Intent
@EActivity public 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; }
onNewIntent能够根据Intent重新注入Extra
@EActivity public class MyActivity extends Activity { @Extra("myStringExtra") String myMessage; @Override protected void onNewIntent(Intent intent) { setIntent(intent); } }
@SystemService
no more Context.getSystemService()
@EActivity public class MyActivity extends Activity { @SystemService NotificationManager notificationManager; }
@SharedPref:
定义:
@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();
@DefaultRes(R.string.defaultPrefName) String resourceName(); @DefaultRes String defaultPrefAge();
}
使用:
@EActivity public class MyActivity extends Activity { @Pref MyPrefs_ myPrefs; } // 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);