android annotation快速简单入门
来源:互联网 发布:java jvm原理 一句话 编辑:程序博客网 时间:2024/06/11 19:34
最近项目,android客户端的,不知道老大发什么神经了,居然将xUtils不用了,注解用了androidannotation,又不能说不会,唉,真是苦逼了我这菜鸟啊。花了点时间简单学习一下,做个笔记。
1.先下载一个Androidannotation的jar吧,这个必须的啊。
1.src下新建目录 complie-libs,将androidannotations-3.2.jar放入其中,右击
在其子项目中Factory Path中add JARs,选择添加的androidannotations-3.2.jar。然后一路apply即可。
2.导入androidannotations-api-3.2.jar到lib目录下,右键build path --> add jar。
3.新建工程,准备测试。android项目
mainActivity的xml配置:也就两个按钮,一个是进入SecondActivity,一个是进入Service:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="open activity" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="open service" /></LinearLayout>现在我们来看MainActivity的代码:
package com.micro.androidannotation;import org.androidannotations.annotations.Click;import org.androidannotations.annotations.EActivity;import org.androidannotations.annotations.ViewById;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.Button;@EActivity(R.layout.activity_main)public class MainActivity extends Activity {@ViewById(R.id.button)public Button button ;@ViewById(R.id.button2)public Button button2 ;@Click(R.id.button)public void startActivity() {Intent intent = new Intent(MainActivity.this , SecondActivity_.class) ;startActivity(intent) ;}@Click(R.id.button2)public void startService(){Intent service = new Intent(MainActivity.this , MyService_.class) ;startService(service) ;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}}在AndroidManifest.xml中配置:
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.micro.androidannotation.MainActivity_" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
好了,一个简单的例子就好了,使用annotation框架就做好了。可以看出使用annotation框架代码非常简洁,基本看不到大量的要命但没什么技术含量的代码了。运行代码就能看到我们想看的界面了。那么我们就好好谈谈它与一般的activity书写上的不同了。
1.我们在MainActivity类上使用了@EActivity注解,表明这是一个标准的activity了。括号中就是对应的layout的Id了。所以我们以前要在onCreate方法中setContentView(R.layout.layout_id)已经不见了。除了@EActivity,annotation还提供了
- @EActivity
- @EApplication
- @EBean
- @EFragment
- @EProvider
- @EReceiver
- @EIntentService
- @EService
- @EView
- @EViewGroup
2.AndroidManifest.xml中Activity的配置。对,你没看错,你必须在每个注册的Activity的名字后面加上一个“_”,这是annotation自动编译你含有注解代码时的生成了java类,也就是这个类才是程序真正运行的类。不仅是Activity,你的Service/Provider/Receiver,如果使用了annotation,那么我们就要遵守它的规定,在注册时加上这个"_". 可能在你project中看不到这个类,右键你project-->properties-->java Complier -->Annotation Procession,如图:
去掉.apt_generated前面的点,点击确定,然后你就会发现你的project下多了apt_generated目录,这个目录下存放了就是annotation将你的含有注解的代码翻译成普通代码的代码了,听起来有点绕,自己看看就行立马知道了。
3.组件的配置和基本方法【所有的组件都必须声明为protected/public的,private的直接报错】
@ViewById == this.findviewbyid(R.id.id_) ;
但是@ViewById中如果你组件变量的名称与组件Id相同,此时可以不写R.id.id_
就像下面:
@ViewById(R.id.button)public Button button ;@ViewByIdpublic Button button ;@ViewByIds,当然还有更牛叉的配置:
@ViewByIdTextView tv_1 ;@ViewByIdTextView tv_2 ;@ViewByIdTextView tv_3 ;@ViewByIdTextView tv_4 ;可以写成:
@ViewsById({R.id.tv_1 , R.id.tv_2 , R.id.tv_3 , R.id.tv_4})List<TextView> listView ;@AfterViews:在oncreate方法执行完毕后,对变量/属性进行初始化的操作
@AfterViewspublic void setText() {for(TextView tv : listView) {tv.setText("lisi") ;}}
@Extra(String_name):用于Activity之间的传值问题,直接上代码:这里写进入的是"Activity_",而不能写Activity了。
SecondActivity中存在:
Intent intent = new Intent(SecondActivity.this , ThirdActivity_.class) ;intent.putExtra("name", "zhangsan") ;intent.putExtra("age", 20) ;startActivity(intent) ;在ThirdActivity中,在afterView方法中就可以获得我们需要的值:
@Extra("name")String name ;@Extra("age")int age ;@AfterViewspublic void afterView(){tv_1.setText("name is " + name) ;tv_2.setText("age is " + age) ;}
4.依赖注入的标签:【所有使用annotation注解的方法都不能使用private修饰】
@click(R.id.id_#)点击事件@LongClick({R.id.id_# , R.id.id_#})长按点击事件
做法就是在某个组件需要执行的方法上填上该注解和该Id即可,如下例子,表明点击button将要从MainActivity中跳转到SecondActivity中
@Click(R.id.button)public void startActivity() {Intent intent = new Intent(MainActivity.this , SecondActivity_.class) ;startActivity(intent) ;}
对于ListView系列含有多item的注入,annotation提供了这样的方式:
@ItemClick(R.id.listview) listview的item点击事件public void itemClick(int position) public void itemClick(Item item)@ItemLongClick(R.id.listview) listview的item长按点击事件public void itemLongClick(int position) public void itemLongClick(Item item)
看到这里,我想你应该明白了它参数的含义了,对此,官方的解释为:Methods annotated with @ItemClick or @ItemLongClick must have one parameter. This parameter can be of any type, it's the object retrieved when calling adapter.getItem(position). 虽然官方文档说明至少要有一个参数,当然你也可以不传入参数,也不报错;但是对于实际开发而言,不传入参数即对于所有的item进行统一处理,基本上也没啥意义了。也就是当传入int的参数时,会被解析成listView系列的position列,当你填入ListView中item时,相当于listview的adapter中adapter.getItem(position)。
同样,还一个比较常用的:
@ItemSelect(R.id.listview)public void isItemSelect(boolean isChoose , Item item)它存在两个参数,第一个boolean型表明该item选中的状态,第儿个参数相当于listview的adapter中adapter.getItem(position),与itemClick中相似。当然annotation不仅仅提供了以上几个注解标签,它还有以下标签:用法大体一致。
- @TextChange
- @AfterTextChange
- @BeforeTextChange
- @EditorAction
- @FocusChange
- @CheckedChange
- @Touch
- @Click
- @LongClick
- @ItemClick
- @ItemLongClick
- @ItemSelect
- @OptionsItem
- @SeekBarProgressChange
- @SeekBarTouchStart
- @SeekBarTouchStop
- @KeyDown
- @KeyUp
- @KeyLongPress
- @KeyMultiple
5.线程标签
使用这个标签基本上可以告别了hanlder那种繁琐的写法了,简单到极致:
先来两个小的方法:
<pre name="code" class="java">//子线程中运行@Backgroundpublic void runInNotUIThread() {System.out.println("this current Thread is " + Thread.currentThread().getId());runInUIThread() ;}//主线程中运行@UiThreadpublic void runInUIThread(){System.out.println("this current Thread is " + Thread.currentThread().getId());}
当你打印时:
你就会发现,加上这两个标签之后,我们的方法就可以运行在不同的线程了,是不是很神奇?同时我们也避免了去写runOnUIThread/handler.post/hander.sendMessage()等等非常繁琐的更新UI的步骤了,是不是很牛啊?
对于线程标签,annotation提供了一下四个注解,基本够我们开发时用:
- @Background
- @UiThread
- @SupposeBackground 【如果不在后台[非主]线程中执行,将抛出异常】
- @SupposeUiThread 【如果不在主线程中执行,将抛出异常】
1 0
- android annotation快速简单入门
- 自定义java Annotation快速入门与提高
- Anjular简单快速入门
- Struts2最新版(2.5.12)快速入门(三) Struts2之Annotation
- 最简单的android annotation demo
- android annotation配置及简单使用
- Android Annotation的一些简单使用
- annotation入门
- Android快速入门
- Android快速入门
- Android模拟器快速入门
- android快速入门指南
- android 快速入门
- Android快速学习入门
- Android Studio 快速入门
- 1.Android快速入门
- Android如何快速入门
- Android.mk 快速入门
- Android中View not attached to window manager错误的解决办法
- runloopObserver
- IOS--设置圆角Button
- runloop的基本使用
- SQL学习笔记(2)之DDL语句
- android annotation快速简单入门
- phpstorm9安装
- hdu 2063
- 快速傅里叶变换算法探幽
- UI--手势方法介绍
- JAVA垃圾回收与GC浅谈
- C++构造函数初始化列表 委托构造函数
- ios工作笔记
- 软件性能测试的几种方法