`
jsntghf
  • 浏览: 2512585 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

AndroidAnnotations的使用

阅读更多

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 {

	@ViewById
	TextView titleTextView;
	@ViewById
	EditText nameEditText;
	@ViewById
	EditText passwordEditText;
	@ViewById
	TextView loginTextView;

	@AfterViews
	void updateTitle() {
		titleTextView.setText("Annotation");
	}

	@Click
	void 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>

 

分享到:
评论

相关推荐

    AndroidAnnotations使用

    AndroidAnnotations(简称AA)是一个开源框架,旨在简化Android开发,通过使用Java注解来消除样板代码,提高代码的可读性和可维护性。它可以帮助开发者快速实现如Activity、Fragment、Intent、EventBus等常见操作,...

    Android Annotations框架使用实例

    Android Annotations框架是一个强大的工具,它通过使用Java注解(Annotations)来简化Android应用的开发,提高了编码效率。本文将深入探讨Android Annotations的使用方法和优势,帮助开发者更好地理解和运用这一框架...

    androidannotations 注解框架的配置及使用教程

    下面将详细介绍AndroidAnnotations的配置与使用步骤。 **配置AndroidAnnotations** 1. **添加依赖**: 首先,你需要在项目的build.gradle文件中添加AndroidAnnotations的依赖。通常,你会在app模块的build.gradle中...

    androidannotations框架举例

    在本例中,我们将深入探讨如何使用AndroidAnnotations框架来提升开发效率。 1. **注解驱动开发** AndroidAnnotations的核心理念是使用注解来声明代码意图,而不是手动编写大量的样板代码。例如,你可以使用`@...

    androidannotations框架最新版

    AndroidAnnotations 提供了对 Eclipse 的良好支持,通过插件可以在 Eclipse 中无缝使用这个框架,便于那些习惯于 Eclipse 的开发者进行开发。 ### 6. 支持 AndroidX 随着 AndroidX 库的广泛使用,...

    androidannotations-3.0.1.jar androidannotations-api-3.0.1.jar

    使用AndroidAnnotations时,需要注意遵循最佳实践,如合理划分注解的使用范围,避免过度使用,以及保持代码的可读性和可维护性。 总结来说,AndroidAnnotations是一个强大的开发工具,通过注解简化了Android应用的...

    androidannotations-bundle-3.0.1.zip

    总的来说,"androidannotations-bundle-3.0.1.zip"这个压缩包为开发者提供了了解和使用AndroidAnnotations库的全面资源。通过学习和使用这个库,开发者不仅可以提升开发效率,还能深入了解Android应用的构建原理,对...

    AndroidAnnotations切换fragment动画

    在`AndroidAnnotations`中,你可以使用`@AfterViews`注解来标识初始化视图后执行的代码,这正是设置`ViewPager`适配器和动画的好时机。你可以创建一个`PagerAdapter`子类,将所有的`Fragment`添加到适配器中,然后...

    androidannotations 测试代码

    虽然不是AndroidAnnotations的一部分,但Dagger2经常与之配合使用,提供依赖注入功能,简化组件之间的依赖关系。在测试中,Dagger2可以帮助我们轻松地管理依赖,创建测试环境。 **9. 测试覆盖率** 为了确保测试的...

    androidannotations-3.2Jar包

    **AndroidAnnotations 框架详解** AndroidAnnotations 是一个强大的开源框架,它为 Android 开发者提供了基于注解的编程方式,极大地提升了开发效率和代码的可读性。这个框架允许开发者将大量的样板代码...

    AndroidAnnotations-3.2

    AndroidAnnotations 提供了一系列预定义的注解,开发者可以使用它们来处理常见的Android编程任务,如活动(Activity)、服务(Service)、广播接收器(BroadcastReceiver)的生命周期管理,视图注入(View Injection...

    androidannotations

    使用AndroidAnnotations,开发者可以更加专注于业务逻辑,提高开发效率,降低出错率,从而提升整个项目的质量和开发体验。因此,无论你是新手还是经验丰富的开发者,AndroidAnnotations都值得你纳入开发工具箱,成为...

    使用AndroidAnnotations框架优雅地实现ListView功能例子源代码

    为了解决这个问题,开发者们引入了各种库,其中AndroidAnnotations(简称AA)是一个强大的工具,能够简化Android开发中的许多常见任务,包括ListView的实现。本文将详细介绍如何利用AndroidAnnotations框架优雅地...

    AndroidAnnotations 是如何工作的

    AndroidAnnotations是Android平台上一种使用注解(Annotation)来简化Android应用开发的库。它通过在源代码中加入注解的方式来自动完成一些常见的开发任务,比如视图的初始化和依赖注入等,从而提高开发效率和减少...

    AndroidAnnotations

    通过使用注解,开发者可以减少样板代码,将更多的精力集中在业务逻辑上。这个框架通过自动化处理常见的开发任务,如视图绑定、资源管理、网络请求等,从而提高开发效率。 核心特性 1. 视图注入 AndroidAnnotations...

    AndroidAnnotations框架的部署及实现过程

    在开始使用AndroidAnnotations之前,首先需要将其添加到项目的构建系统中。如果你使用的是Gradle,可以在`build.gradle`文件的`dependencies`部分添加以下依赖: ```groovy dependencies { // AndroidAnnotations...

    AndroidAnnotations相关jar包绿色解压版

    在编译时,注解处理器会扫描使用了AndroidAnnotations注解的代码,生成相应的Java字节码,这些字节码在运行时被加载,实现了注解声明的功能。这个过程被称为APT(Annotation Processing Tool)或者APT框架。这个过程...

    基于AndroidAnnotations的demo

    虽然 AndroidAnnotations 并不直接处理网络请求,但它与诸如 Retrofit、Volley 等网络库结合使用时,能提供更好的抽象层,简化网络请求的代码。 **7. 示例项目** “TestAA” 文件可能是一个简单的示例应用,展示了...

    androidannotations-3.2

    本文将深入探讨AndroidAnnotations 3.2版本中的核心概念、使用方法及其优势。 **注解驱动的开发** 注解在Java语言中是一种元数据,它可以提供额外的信息来修饰类、接口、方法等。AndroidAnnotations利用注解的力量...

Global site tag (gtag.js) - Google Analytics