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

使用AndroidAnnotations,相比原生的Android开发,确实能够让你少写很多代码,它的首页也给出了一个简单

的例子,通过例子也可以看到代码比之前几乎少写了一半。

AndroidAnnotations官方主页:http://androidannotations.org/

首先,看看AndroidAnnotations如何集成。

1、到官网下载AndroidAnnotations的jar包,本文使用的是AndroidAnnotations 3.0.1版本。

解压后将androidannotations-3.0.1.jar加入项目的libs目录。

2、配置Eclipse

(1)项目右键选择Properties,选择Java Compiler,确保编译器版本为1.6。

(2)Java Compiler -> Annotation Processing -> Enable annotation processing

(3)Java Compiler -> Annotation Processing -> Enable annotation processing -> Factory Path -> 添加androidannotations-3.0.1.jar

(4)Project -> Clean

接下来,给出一个简单的例子,通过这个例子,可以对AndroidAnnotations的使用有个初步的了解。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/gray_bg"    android:orientation="vertical" >    <TextView        android:id="@+id/titleTextView"        android:layout_width="match_parent"        android:layout_height="45.0dp"        android:background="@color/titlebar_bg"        android:gravity="center"        android:singleLine="true"        android:textColor="@color/white"        android:textSize="22.0sp"        android:textStyle="normal" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_margin="10.0dp"        android:background="@drawable/frame"        android:orientation="vertical" >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center_vertical"            android:orientation="horizontal"            android:padding="10.0dp" >            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/icon_touxiang" />            <TextView                android:layout_width="60.0dp"                android:layout_height="wrap_content"                android:layout_gravity="center_vertical"                android:layout_marginLeft="10.0dp"                android:text="账号:"                android:textColor="@color/text_black"                android:textSize="15.0sp" />            <EditText                android:id="@+id/nameEditText"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:background="@null" />        </LinearLayout>        <View            android:layout_width="match_parent"            android:layout_height="1.0px"            android:background="@color/line_normal" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:gravity="center_vertical"            android:orientation="horizontal"            android:padding="10.0dp" >            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:src="@drawable/icon_mima" />            <TextView                android:layout_width="60.0dp"                android:layout_height="wrap_content"                android:layout_gravity="center_vertical"                android:layout_marginLeft="10.0dp"                android:text="密码:"                android:textColor="@color/text_black"                android:textSize="15.0sp" />            <EditText                android:id="@+id/passwordEditText"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:background="@null"                android:password="true" />        </LinearLayout>    </LinearLayout>    <TextView        android:id="@+id/loginTextView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginBottom="20.0dp"        android:layout_marginLeft="10.0dp"        android:layout_marginRight="10.0dp"        android:layout_marginTop="10.0dp"        android:background="@drawable/blue_bground"        android:gravity="center"        android:padding="10.0dp"        android:text="登录"        android:textColor="@color/white"        android:textSize="15.0sp" /></LinearLayout>

MainActivity.java

package com.eric.annotation;import org.androidannotations.annotations.AfterViews;import org.androidannotations.annotations.Click;import org.androidannotations.annotations.EActivity;import org.androidannotations.annotations.ViewById;import android.app.Activity;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;@EActivity(R.layout.activity_main)public class MainActivity extends Activity {@ViewByIdTextView titleTextView;@ViewByIdEditText nameEditText;@ViewByIdEditText passwordEditText;@ViewByIdTextView loginTextView;@AfterViewsvoid updateTitle() {titleTextView.setText("Annotation");}@Clickvoid loginTextView() {Toast.makeText(getApplicationContext(),"name:" + nameEditText.getText() + "\npassword:" + passwordEditText.getText(), Toast.LENGTH_SHORT).show();}}

注意:AndroidManifest.xml文件里的Activity的名字都要在原来的基础上加一个下划线 。例如:

<activity android:name="com.eric.annotation.MainActivity"></activity>

改成

<activity android:name="com.eric.annotation.MainActivity_"></activity>

更多相关文章

  1. Kotlin 如何实现复杂序列化Parcelable
  2. Android(安卓)studio使用git教程
  3. Android(安卓)ViewFlipper 用例
  4. Andorid TabHost 使用小结
  5. Android(安卓)GridView控件 使用
  6. Android打印日志管理
  7. Android资料(书籍+代码)
  8. 完美PopupWindow(记住用户名模拟)
  9. Android支付Demo运行大坑

随机推荐

  1. Android链式方法显示Dialog
  2. 【Android(安卓)总结】缩写
  3. 14 Android(安卓)android 按钮效果的两种
  4. [置顶] androidの下拉菜单Spinner使用
  5. Android(安卓)实现部分文字高亮的三种方
  6. Android中如何判断是否联网
  7. 【Android-File】Android文件的读写
  8. Android(安卓)pthread mutex 实现分析
  9. android 渐变背景
  10. Android(安卓)Error:AAPT: error: resourc