在Android项目中使用AndroidAnnotations(配置框架,显示Hello World!)

时间:2023-03-10 06:22:37
在Android项目中使用AndroidAnnotations(配置框架,显示Hello World!)

使用这个框架可以极大的简化在开发Android过程中的代码.提高开发的效率.这里简单说一下配置方式.和使用办法.

项目的地址为:AndroidAnnotations

Jar包下载地址:3.0.1

下载打开后可以看到有两个jar.一个是androidannotaions-x.x.x.jar,一个是androidannotaions-api-x.x.x.jar

  • androidannotations-x.x.x.jar是用来提供给java的注解处理插件
  • androidannotations-api-x.x.x.jar是给android工程使用的包

在Android项目中使用AndroidAnnotations(配置框架,显示Hello World!)

接下来就是配置你现有的Android项目.可以使用这个框架.

首先要确认你sdk版本在1.6以上.

开始配置:

          • 将androidannotations-X.X.X-api.jar文件拷贝到你的libs文件夹下
          • 将androidannotations-X.X.X.jar拷贝到其他文件夹下.例如compile-libs,一定要保证androidannotations-X.X.X.jar不在libs文件夹下.
          • 项目文件上右键单击”属性(Properties)”
          • 找到”Java Compiler”确定编译级别为1.6
          • 找到”Java Compiler > Annotation Processing”选择”Enable annotation processing”在Android项目中使用AndroidAnnotations(配置框架,显示Hello World!)
          • 找到“Java Compiler > Annotation Processing > Factory Path”添加androidannotations-X.X.X.jar在Android项目中使用AndroidAnnotations(配置框架,显示Hello World!)
          • 找到”Java Build Path > Libraries”如果build path中没有androidannotations-X.X.X-api.jar.添加那个api文件在Android项目中使用AndroidAnnotations(配置框架,显示Hello World!)
          • 在Android项目中使用AndroidAnnotations(配置框架,显示Hello World!)

OK,到了这步配置就完成了.

那接下来简单说一下如何使用.

先看一下官方的示例.测试一下你的配置是否正确.

修改你的activity_main.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <EditText
        android:id="@+id/myInput"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/myButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Click me!"
        />
    <TextView
        android:id="@+id/myTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

修改你的MainActivity.java

@EActivity(R.layout.activity_main)
public class MyActivity extends Activity {

    @ViewById(R.id.myInput)
    EditText myInput;

    @ViewById(R.id.myTextView)
    TextView textView;

    @Click
    void myButton() {
         String name = myInput.getText().toString();
         textView.setText("Hello "+name);
    }
}

然后要注意了:需要修改AndroidManifest.xml.使用此框架需要在你原有的activity添加后缀:”_”

<activity android:name=".MainActivity_" />