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

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

问题描述

我想了解Android注释,这是在所有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注释的缺点是什么?

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

预先感谢您的帮助.

推荐答案

Android注释是注释驱动的框架,可让您简化应用程序中的代码并减少常见模式的样板,例如设置点击监听器,强制执行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注释的教程,甚至还提供了有关如何更新集成测试的示例,在此处检查.

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注释的运行方式,它们将自己嵌入生命周期中,因此,您现在依赖于生命周期(例如,如果您使用@ViewById注释视图,则无法在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注释?它们的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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