什么是 Android Annotations,它们的用途是什么? [英] What are Android Annotations and what are they used for?

查看:32
本文介绍了什么是 Android Annotations,它们的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解 Android Annotations,它是不是在所有 android 项目中都更好地使用?

I want to know about Android Annotations, is it better way to use in all android projects?.

如果正确,如何实现.有什么好的教程吗?

If correct, how to implement it. Is there any good tutorial for it?

如果方法不对.Android Annotations 的缺点是什么?

If it is a wrong way. what are the drawbacks of Android Annotations?

在此先感谢您的帮助.

推荐答案

Android Annotations 是一个注解驱动的框架,它允许您简化应用程序中的代码并减少常见模式的样板,比如设置点击监听,强制ui/后台线程执行等

Android Annotations is an annotation-driven framework that allows you to simplify the code in your applications and reduces the boilerplate of common patterns, such as setting click listeners, enforcing ui/background thread executions, etc.

你可以拥有这样的东西:

You could go from having something like this:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView descriptionTextView = (TextView) findViewById(R.id.tv_description);
        final Button hideButton = (Button) findViewById(R.id.btn_hide);
        hideButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                descriptionTextView.setVisibility(View.INVISIBLE);
            }
        });
    }
}

这样的事情:

@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @ViewById(R.id.tv_description)
    TextView mDescriptionTextView;

    @Click(R.id.btn_hide)
    protected void onHideButtonClick() {
        mDescriptionTextView.setVisibility(View.INVISIBLE);
    }
}

工作原理

您注释您的活动和组件,然后注释处理器生成类(在编译时)扩展您的活动和组件(即您的活动不能是最终的),默认情况下带有下划线后缀,因此如果您有 MainActivity,现在您还将拥有一个 MainActivity_ 类.

You annotate your activities and components, the annotations processor then generates classes (at compilation time) that extend your activities and components (i.e. your activities cannot be final) with an underscore suffix by default, so if you have MainActivity, now you will also have a MainActivity_ class.

这个新类包含一个编写良好的样板代码,可以执行注释指定的任何操作.

This new class contains a well-written boilerplate code that does whatever the annotation specifies.

如何实施

我写了这个关于如何集成 Android Annotations 的教程,甚至包括一个关于如何更新集成测试的例子,点击此处.

I wrote this tutorial about how to integrate Android Annotations and even include an example on how integration tests get updated, check here.

该教程从今天起有效,使用的是 Android Studio ~1.5.1,它试图解释一些内部工作原理.

That tutorial is valid as of today, using Android Studio ~1.5.1, and it tries to explain a little bit on how the internal works.

你应该使用它吗?

我会说,如果你有一个中小型项目,那很好.这将使您的代码更易于阅读.但是,如果您的应用程序更大并且包含大量具有复杂活动/组件生命周期的导航流,那么如果某些内容没有正确注释,它可能会有点难以实现或难以调试和理解错误.

I would say that if you have a small-medium project its fine. It will make your code easier to read. But if your application is bigger and contains a lot of navigation flows with complex activity/component life cycles, it can get a little bit hard to implement or difficult to debug and understand errors if something is not appropriately annotated.

由于 Android Annotations 的运作方式,它们将自己嵌入到生命周期中并这样做,您现在依赖于它们的生命周期(例如,如果您使用 @ViewById 注释您的视图code>,那么你就不能在 onCreate() 中引用它们,你需要创建一个方法并用 @AfterViews 注释它,当这个方法被执行时,你的视图就准备好了要使用的).这不一定是问题,您只需要对 Android 的行为以及 Android Annotations 的行为有很好的了解即可.

Because of how Android Annotations operate, they embed themselves in the life cycle and doing so, you are now dependent of their lifecycle (e.g. if you annotate your views with @ViewById, then you cannot reference them in onCreate(), you need to make a method and annotate it with @AfterViews and when this method gets executed then your views are ready to be used). This is not necessarily a problem, you just need to have a good understanding of Android's behaviors and well, Android Annotations behaviors as well.

总而言之,就像在任何库中一样,如果你依赖它,那么你就依赖它,所以你不妨非常彻底地了解它是如何工作的.您的项目现在取决于其他人的.

In summary, as in any library, if you depend on it, well you depend on it, so you might as well understand very thoroughly how it works. Your project now depends on someone else's.

这篇关于什么是 Android Annotations,它们的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆