Android懒人框架Android annotation
来源:互联网 发布:java 适配器模式详解 编辑:程序博客网 时间:2024/05/21 07:50
不知不觉有一个多月没更新了。唉,辞职了,最近一直在找工作没办法。蓝瘦。香菇。
annotation的配置
project的build.gradle
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.5.0' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' }}allprojects { repositories { jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}
moudle的build.gradle配置
apply plugin: 'com.android.application'apply plugin: 'android-apt'def AAVersion = '4.0+'repositories { jcenter() flatDir { dirs 'libs' }}android { compileSdkVersion 19 buildToolsVersion "23.0.2" defaultConfig { applicationId "source.code.watch.film" minSdkVersion 14 targetSdkVersion 14 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } signingConfigs { debug { storeFile file("E:\\psd\\debug.keystore") } }}dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') apt "org.androidannotations:androidannotations:$AAVersion" compile "org.androidannotations:androidannotations-api:$AAVersion"}apt { arguments { androidManifestFile variant.outputs[0].processResources.manifestFile resourcePackageName 'source.code.watch.film' }}
今天和大家介绍下Android的懒人框架:annotation。
@EActivity
@EActivity(R.layout.main)public class MyActivity extends Activity {}
@EFragment
@EFragment(R.layout.my_fragment_layout)public class MyFragment extends Fragment {}
<fragment android:id="@+id/myFragment" android:name="com.company.MyFragment_" android:layout_width="fill_parent" android:layout_height="fill_parent" />
MyFragment fragment = new MyFragment_();
@EBean
public class MyClass {}- 这个类必须仅仅只能有一个构造函数,参数最多有一个context
@EActivity(R.layout.main)public class MyActivity extends Activity { @Bean MyClass myClass;}
@EActivity(R.layout.main)public class MyActivity extends Activity { @Bean(MyClass.class) MyClassInterface myClassInterface;}
@EBeanpublic class MyClass { @RootContext Context context; @RootContext Activity activity; @RootContext Service service; @RootContext MyActivity myActivity;}
@EActivity(R.layout.main)public class MyActivity extends Activity { @AfterInject public void doSomethingAfterInjection() { }}- 如果想在类创建时期做一些操作可以这么做
@EBean(scope = Scope.Singleton)public class MySingleton {}- 单例类- 在单例类里面不可以注入view和事件绑定,因为单例的生命周期比Activity和Service的要长,以免发生内存溢出
@EView
@EViewpublic class CustomButton extends Button { @App MyApplication application; @StringRes String someStringResource; public CustomButton(Context context, AttributeSet attrs) { super(context, attrs); }}
<com.androidannotations.view.CustomButton_ android:layout_width="match_parent" android:layout_height="wrap_content" />
CustomButton button = CustomButton_.build(context);
@EViewGroup
@EViewGroup(R.layout.title_with_subtitle)public class TitleWithSubtitle extends RelativeLayout { @ViewById protected TextView title, subtitle; public TitleWithSubtitle(Context context, AttributeSet attrs) { super(context, attrs); } public void setTexts(String titleText, String subTitleText) { title.setText(titleText); subtitle.setText(subTitleText); }}
<com.androidannotations.viewgroup.TitleWithSubtitle_ android:id="@+id/firstTitle" android:layout_width="match_parent" android:layout_height="wrap_content" />
@EAplication
@EApplicationpublic class MyApplication extends Application {}
@EActivity(R.layout.main)public class MyActivity extends Activity { @App MyApplication application;}
EService
@EServicepublic class MyService extends Service {}
MyService_.intent(getApplication()).start();
MyService_.intent(getApplication()).stop();
@EReceiver
@EReceiverpublic class MyReceiver extends BroadcastReceiver {}
@EActivity(R.layout.main)public class MyActivity extends Activity { @Receiver(actions = "org.androidannotations.ACTION_1") protected void onAction1() { }}
@EProvider
@EProviderpublic class MyContentProvider extends ContentProvider {}
@EViewById
@EActivity(R.layout.main)public class MyActivity extends Activity { @ViewById EditText myEditText; @ViewById(R.id.myTextView) TextView textView;}- 没有括号里定义的变量名称必须和布局的id名称一致
@EAfterViews
@EActivity(R.layout.main)public class MyActivity extends Activity { @ViewById TextView myTextView; @AfterViews void updateTextWithDate() { myTextView.setText("Date: " + new Date()); }} 一定要在这里进行view的一些设置,不要在oncreate()中设置,因为oncreate()在执行时 view还没有注入
@StringRes
@EActivity(R.layout.main)public class MyActivity extends Activity { @StringRes(R.string.hello) String myHelloString; @StringRes String hello;}- 不能将变量设置成私有变量
@ColorRes
@EActivity(R.layout.main)public class MyActivity extends Activity { @ColorRes(R.color.backgroundColor) int someColor; @ColorRes int backgroundColor;}
@AnimationRes
@EActivity(R.layout.main)public class MyActivity extends Activity { @AnimationRes(R.anim.fadein) XmlResourceParser xmlResAnim; @AnimationRes Animation fadein;}
@DimensionRes
@EActivity(R.layout.main)public class MyActivity extends Activity { @DimensionRes(R.dimen.fontsize) float fontSizeDimension; @DimensionRes float fontsize;}
@DimensionPixelOffsetRes
@EActivity(R.layout.main)public class MyActivity extends Activity { @DimensionPixelOffsetRes(R.string.fontsize) int fontSizeDimension; @DimensionPixelOffsetRes int fontsize;}
@DimensionPixelSizeRes
@EActivity(R.layout.main)public class MyActivity extends Activity { @DimensionPixelSizeRes(R.string.fontsize) int fontSizeDimension; @DimensionPixelSizeRes int fontsize;}
@Extra
@EActivity(R.layout.main)public class MyActivity extends Activity { @Extra("myStringExtra") String myMessage; @Extra("myDateExtra") Date myDateExtraWithDefaultValue = new Date();}
@EActivity(R.layout.main)public class MyActivity extends Activity { @Extra String myMessage;}- The name of the extra will be "myMessage",名字必须一致
MyActivity_.intent().myMessage("hello").start() ;
MyActivity_.intent().myMessage("hello").startForResult() ;
@SystemService
@EActivity(R.layout.main)public class MyActivity extends Activity { @SystemService NotificationManager notificationManager;}
@HtmlRes
@EActivity(R.layout.main)public class MyActivity extends Activity { @HtmlRes(R.string.hello_html) Spanned myHelloString; @HtmlRes CharSequence helloHtml;}
@FromHtml
@EActivity(R.layout.main)public class MyActivity extends Activity { @ViewById(R.id.my_text_view) @FromHtml(R.string.hello_html) TextView textView; @ViewById @FromHtml TextView hello_html;}- 必须用在TextView
@NonConfigurationInstance
@EActivity(R.layout.main)public class MyActivity extends Activity { @NonConfigurationInstance Bitmap someBitmap; @NonConfigurationInstance @Bean MyBackgroundTask myBackgroundTask;}- 等同于Activity.onRetainNonConfigurationInstance()
@Click
@Click(R.id.myButton)void myButtonWasClicked() {}@Clickvoid anotherButton() {}@Clickvoid yetAnotherButton(View clickedView) {}- LongClick和这个类似
@SeekBarProgressChange
@SeekBarProgressChange(R.id.seekBar) void onProgressChangeOnSeekBar(SeekBar seekBar, int progress, boolean fromUser) { } @SeekBarProgressChange(R.id.seekBar) void onProgressChangeOnSeekBar(SeekBar seekBar, int progress) { } @SeekBarProgressChange({R.id.seekBar1, R.id.seekBar2}) void onProgressChangeOnSeekBar(SeekBar seekBar) { } @SeekBarProgressChange({R.id.seekBar1, R.id.seekBar2}) void onProgressChangeOnSeekBar() { }- @SeekBarTouchStart 和 @SeekBarTouchStop 接受开始和结束事件的监听
@TextChange
@TextChange(R.id.helloTextView) void onTextChangesOnHelloTextView(CharSequence text, TextView hello, int before, int start, int count) { } @TextChange void helloTextViewTextChanged(TextView hello) { } @TextChange({R.id.editText, R.id.helloTextView}) void onTextChangesOnSomeTextViews(TextView tv, CharSequence text) { } @TextChange(R.id.helloTextView) void onTextChangesOnHelloTextView() { }
@BeforeTextChange
@BeforeTextChange(R.id.helloTextView) void beforeTextChangedOnHelloTextView(TextView hello, CharSequence text, int start, int count, int after) { } @BeforeTextChange void helloTextViewBeforeTextChanged(TextView hello) { } @BeforeTextChange({R.id.editText, R.id.helloTextView}) void beforeTextChangedOnSomeTextViews(TextView tv, CharSequence text) { } @BeforeTextChange(R.id.helloTextView) void beforeTextChangedOnHelloTextView() { }
@AfterTextChange
@AfterTextChange(R.id.helloTextView) void afterTextChangedOnHelloTextView(Editable text, TextView hello) { } @AfterTextChange void helloTextViewAfterTextChanged(TextView hello) { } @AfterTextChange({R.id.editText, R.id.helloTextView}) void afterTextChangedOnSomeTextViews(TextView tv, Editable text) { } @AfterTextChange(R.id.helloTextView) void afterTextChangedOnHelloTextView() { }
@Background
void myMethod() { someBackgroundWork("hello", 42);}@Backgroundvoid someBackgroundWork(String aParam, long anotherParam) {}- 后台运行
void myMethod() { someCancellableBackground("hello", 42); boolean mayInterruptIfRunning = true; BackgroundExecutor.cancelAll("cancellable_task", mayInterruptIfRunning);}@Background(id="cancellable_task")void someCancellableBackground(String aParam, long anotherParam) {}
void myMethod() { for (int i = 0; i < 10; i++) someSequentialBackgroundMethod(i);}@Background(serial = "test")void someSequentialBackgroundMethod(int i) { SystemClock.sleep(new Random().nextInt(2000)+1000); Log.d("AA", "value : " + i);}- 非并发执行
@Background(delay=2000)void doInBackgroundAfterTwoSeconds() {}- 延迟执行
@UIThread
void myMethod() { doInUiThread("hello", 42);}@UiThreadvoid doInUiThread(String aParam, long anotherParam) {}- UI线程
@UiThread(delay=2000)void doInUiThreadAfterTwoSeconds() {}- 延迟
@UiThread(propagation = Propagation.REUSE)void runInSameThreadIfOnUiThread() {}- 优化UI线程
@EActivity(R.layout.main)public class MyActivity extends Activity { @Background void doSomeStuffInBackground() { publishProgress(0); publishProgress(10); publishProgress(100); } @UiThread void publishProgress(int progress) { }}- 后台向UI线程传值
@OnActivityResult
@OnActivityResult(REQUEST_CODE)void onResult(int resultCode, Intent data) {}@OnActivityResult(REQUEST_CODE)void onResult(int resultCode) {}@OnActivityResult(ANOTHER_REQUEST_CODE)void onResult(Intent data) {}@OnActivityResult(ANOTHER_REQUEST_CODE)void onResult() {}
好了。关于annotation的介就是这么多。欢迎进群交流。还有,求工作啊!!!!
1 0
- Android懒人框架Android annotation
- 懒人必备 之 Android效率开发框架
- 推荐一个Android开发,懒人框架ButterKnife。
- Android Annotation框架初步实践
- 懒人必备之Android效率开发框架(伪原创)
- Android之懒人框架 ButterKnife 8.4添加使用
- android懒人笔记
- android 个人懒人笔记
- Android懒人库----ButterKnife
- Android懒人开发mark
- Android开发 打造自己的Annotation框架
- 利用Annotation实现Android sqlite框架
- android annotation
- Android -- Annotation
- Android Annotation
- android码农神器 偷懒工具 android懒人框架 LoonAndroid 3 讲解
- Android开发懒人库 -- ButterKnife
- Android开发懒人库 -- ButterKnife
- JavaScript基础_04对象与函数
- YYUC01——Windows本地环境搭建
- JQuery--放大镜效果
- 运算符
- HTTP与NRPC
- Android懒人框架Android annotation
- 第七周项目2建立链队算法库
- 用bootstrap写一个简洁的网页
- Android Studio插件开发实践--从创建到发布
- Hibernate 建立双向多对多关联关系
- 写给进军大数据的你们:
- MySQL聚合函数
- 【leetcode】63. Unique Paths II
- Android -- Handler消息机制简析