androidannotation 是github上的一个开源项目。

主要是注解机制,可以改善android写代码的效率。

Activity 使用

1.@EActivity 注解

可想而知,service,broadcastreceiver等都有相应的注解。

package com.joyfulmath.myannnotationsample.activity;

import android.app.Activity;
import com.joyfulmath.myannnotationsample.R;
import com.joyfulmath.myannnotationsample.fragment.EFragmentSampleFragment;
import com.joyfulmath.myannnotationsample.utils.TraceLog; import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.FragmentById;
import org.androidannotations.annotations.Receiver; @EActivity(R.layout.activity_eactivity_sample)
public class EActivitySampleActivity extends Activity { @FragmentById(R.id.myFragemnt)
EFragmentSampleFragment eFragmentSampleFragment; @Receiver(actions = EFragmentSampleFragment.ACTION)
void myReceiver()
{
TraceLog.i("receive:"+EFragmentSampleFragment.ACTION);
}
}

一个典型的Activity如上注解。R.layout.activity_eactivity_sample

这个layout表示,该activity讲加载这个layout,等同于在oncreate中setcontent一样。

@FragmentById 是fragment的注解,后面会讲到。

@Receiver使用的是 监听广播。比起老的写法,大大简化。

2.@EFragment

package com.joyfulmath.myannnotationsample.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView; import com.joyfulmath.myannnotationsample.R;
import com.joyfulmath.myannnotationsample.utils.TraceLog;
import com.joyfulmath.myannnotationsample.view.EViewSampleView; import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.RootContext;
import org.androidannotations.annotations.ViewById; /**
* Created by deman on 2015/11/5.
*/
@EFragment(R.layout.fragment_efragment_sample)
public class EFragmentSampleFragment extends BaseFragment { public final static String ACTION = "efagment.samplefragment.action"; @ViewById(R.id.textFragmentView)
TextView textView; @ViewById(R.id.eViewSample)
EViewSampleView eViewSampleView; @Click(R.id.textFragmentView)
void sendBroadCast()
{
eViewSampleView.setVisibility(View.VISIBLE);
eViewSampleView.setSpannableText("custom define text");
doBackground();
} @Background
void doBackground()
{
TraceLog.i("sendBroadCast");
getActivity().sendBroadcast(new Intent(ACTION));
}
}

与activity类似,@EFragment 后面也是带一个layout,来表示这个fragment的布局。

里面的布局元素,使用也是非常简单:

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

这句话的意思就是,在适当的时候把

textView = ((TextView) hasViews.findViewById(com.joyfulmath.myannnotationsample.R.id.textFragmentView));

在\app\build\generated\source\apt\debug\com\joyfulmath\myannnotationsample\fragment

下面会生成类+“_”.

这个类才是实际真正运行的类。

当然要运用fragment,和老的写法是一致的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.joyfulmath.myannnotationsample.activity.EActivitySampleActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/myFragemnt"
class="com.joyfulmath.myannnotationsample.fragment.EFragmentSampleFragment_"
/>
</RelativeLayout>

3.Custom Class

这个写法更加简单:@EBean

用这个修饰词,表示class的构造只有默认,以及带context参数的2种结构。

在其他地方使用这个calss的时候,使用@Bean注解。

@EBean
public class NotificationMgr { @SystemService
NotificationManager notificationManager; @RootContext
Context context;

看到@RootContext 是构造函数用的context。

看使用的地方,debug里面可以看到:

notificationMgr = NotificationMgr_.getInstance_(this);//this  就是当前activity

4.custom view

自定义view的关键是2点:

@EView关键字

构造函数如下:

@EView
public class EViewSampleView extends TextView { public EViewSampleView(Context context,AttributeSet attributes)
{
super(context,attributes);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
} public void setSpannableText(CharSequence text)
{
TraceLog.i(text.toString());
SpannableString spannableString = new SpannableString(text);
spannableString.setSpan(new ForegroundColorSpan(Color.GREEN),1,spannableString.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
setText(spannableString);
}
}

其他就和自定义view一样了。

参考:

https://github.com/excilys/androidannotations/wiki/Cookbook

androidannotation study(1)---Activity, Fragment,Custom Class & Custom View的更多相关文章

  1. 适配器(adapter)与fragment之间、fragment与activity之间的通信问题

    一.适配器(adapter)与fragment之间通信 通过本地广播进行通信 步骤如下 在adapter中代码 声明本地广播管理 private LocalBroadcastManager local ...

  2. Android开发教程 - 使用Data Binding(四)在Fragment中的使用

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  3. 【转】 Pro Android学习笔记(三八):Fragment(3):基础小例子-续

    目录(?)[-] Step 2实现Fragment指定调用类TitleFragment onInflate和onAttach onCreate和onCreateView onActivityCreat ...

  4. Android之Activity系列总结(一)--Activity概览

    Activity 本文内容 创建 Activity 实现用户界面 在清单文件中声明 Activity 启动 Activity 启动 Activity 以获得结果 结束 Activity 管理 Acti ...

  5. Android基础总结(六)Activity

    创建第二个Activity(掌握) 需要在清单文件中为其配置一个activity标签 标签中如果带有这个子节点,则会在系统中多创建一个快捷图标 <intent-filter> <ac ...

  6. android源码解析(十七)-->Activity布局加载流程

    版权声明:本文为博主原创文章,未经博主允许不得转载. 好吧,终于要开始讲讲Activity的布局加载流程了,大家都知道在Android体系中Activity扮演了一个界面展示的角色,这也是它与andr ...

  7. ViewPager 详解(五)-----使用Fragment实现ViewPager滑动

    前言:前几篇文章讲解了ViewPager的普通实现方法,但Android官方最推荐的一种实现方法却是使用fragment,下面我们使用fragment来重新实现一下第一篇<ViewPager 详 ...

  8. 02Android用户界面优化之(一)Android Fragment

    一.使用Fragment 1.AndroidManifest.xml文件 <?xml version="1.0" encoding="utf-8"?> ...

  9. Android四大组件全然解析(一)---Activity

    本文參考\android\android\frameworks\base\core\java\android\app\Activity.java文件里的类凝视.以及android/frameworks ...

随机推荐

  1. Java hashCode() 和 equals()的若干问题解答

    本章的内容主要解决下面几个问题: 1 equals() 的作用是什么? 2 equals() 与 == 的区别是什么? 3 hashCode() 的作用是什么? 4 hashCode() 和 equa ...

  2. FAQ: Machine Learning: What and How

    What: 就是将统计学算法作为理论,计算机作为工具,解决问题.statistic Algorithm. How: 如何成为菜鸟一枚? http://www.quora.com/How-can-a-b ...

  3. Unity3D 敌人AI 和 动画( Animator )系统的实例讲解

    在这个实例中,我们要做一些敌人AI的简单实现,其中自动跟随和动画是重点,我们要达到的目标如下: 1.敌人能够自动跟随主角 2.敌人模型一共有四个动作:Idle(空闲) Run(奔跑) Attack(攻 ...

  4. elasticsearch配置

    配置文件详解1.0版 配置文件位于es根目录的config目录下面,有elasticsearch.yml和logging.yml两个配置,主配置文件是elasticsearch.yml,日志配置文件是 ...

  5. 关于Latch争用

    Latch是什么     Latch是SQL Server引擎保证内存中的结构的一致性的轻量同步机制.比如索引,数据页和内部结构(比如非叶级索引页).SQL Server使用Buffer Latch保 ...

  6. MySQL5中大数据错误:Packet for query is too large (****** > ******). You can change this value on the server by setting the max_allowed_packet' variable.;

    使用的MySQL数据库版本:5.5 插入或更新字段有大数据时(大于1M),会出现如下错误: ### Cause: com.mysql.jdbc.PacketTooBigException: Packe ...

  7. P6 EPPM 16.1 安装和配置指南 1

    安装和配置指南下一topiccontents这些指南解释如何安装和配置数据库服务器,和P6 EPPM,模块:他们还提供在P6 EPPM能够解决所有模块的概述.标准指南帮助您配置和部署应用程序向导P6 ...

  8. C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表

    调用word的com组件将400条数据导入word表格中耗时10分钟简直不能忍受,使用NPOI组件耗时4秒钟.但是NPOI中替换书签内容的功能不知道是不支持还是没找到. 辅助类 Excel表格数据与D ...

  9. 基于FreeBSD 64位内核的kFreeBSD无法在Virtualbox下安装

    ArchBSD同上 感谢大A(豆瓣)的投稿 :)

  10. 咋一看DWoo 比 Smarty要好

    虽然很少用模板引擎,但总是有要用到的时候. 随意翻看了两者代码,发现Smarty发展了这么多年居然还在用Eval实现一些特性.其实这没有什么高不高级之分,只是因为eval这个东东,导致一旦语法出错时, ...