`
jsntghf
  • 浏览: 2478770 次
  • 性别: 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>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics