androidannotation study(1)---Activity, Fragment,Custom Class & Custom View
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的更多相关文章
- 适配器(adapter)与fragment之间、fragment与activity之间的通信问题
一.适配器(adapter)与fragment之间通信 通过本地广播进行通信 步骤如下 在adapter中代码 声明本地广播管理 private LocalBroadcastManager local ...
- Android开发教程 - 使用Data Binding(四)在Fragment中的使用
本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...
- 【转】 Pro Android学习笔记(三八):Fragment(3):基础小例子-续
目录(?)[-] Step 2实现Fragment指定调用类TitleFragment onInflate和onAttach onCreate和onCreateView onActivityCreat ...
- Android之Activity系列总结(一)--Activity概览
Activity 本文内容 创建 Activity 实现用户界面 在清单文件中声明 Activity 启动 Activity 启动 Activity 以获得结果 结束 Activity 管理 Acti ...
- Android基础总结(六)Activity
创建第二个Activity(掌握) 需要在清单文件中为其配置一个activity标签 标签中如果带有这个子节点,则会在系统中多创建一个快捷图标 <intent-filter> <ac ...
- android源码解析(十七)-->Activity布局加载流程
版权声明:本文为博主原创文章,未经博主允许不得转载. 好吧,终于要开始讲讲Activity的布局加载流程了,大家都知道在Android体系中Activity扮演了一个界面展示的角色,这也是它与andr ...
- ViewPager 详解(五)-----使用Fragment实现ViewPager滑动
前言:前几篇文章讲解了ViewPager的普通实现方法,但Android官方最推荐的一种实现方法却是使用fragment,下面我们使用fragment来重新实现一下第一篇<ViewPager 详 ...
- 02Android用户界面优化之(一)Android Fragment
一.使用Fragment 1.AndroidManifest.xml文件 <?xml version="1.0" encoding="utf-8"?> ...
- Android四大组件全然解析(一)---Activity
本文參考\android\android\frameworks\base\core\java\android\app\Activity.java文件里的类凝视.以及android/frameworks ...
随机推荐
- Hadoop入门进阶课程13--Chukwa介绍与安装部署
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,博主为石山园,博客地址为 http://www.cnblogs.com/shishanyuan ...
- Win7以上 32/64位系统隐藏托盘图标
前两天有朋友找我,让帮忙写个小工具,隐藏windows的托盘图标,想想最近在家也不想做太复杂的事,也好几年没写过windows上的小工具了,就答应了.想来挺简单的事,没想到还是有点小插曲的.特地来博客 ...
- Vim杂记:markdown插件
一.安装插件 从以下地址下载压缩包 github下载:https://github.com/plasticboy/vim-markdown plasticboy下载:http://plasticboy ...
- 深度使用react-native的热更新能力,必须知道的一个shell命令
开篇之前,先讲一个自己开发中的一个小插曲: 今天周日,iOS版 App 周一提交,周三审核通过上架,很给力.不过,中午11:30的时候,运营就反应某个页面有一个很明显的问题,页面没法拉到底部,部分信息 ...
- vs2012中编译时出现程序集所使用的版本高于所引用的版本
我在运行别人的项目时出现版本不兼容的问题:
- 做java工作整整1年了,看到了大牛的奋斗历程,我感觉自己又有目标了
2014年6月,刚刚踏上社会那会儿,我记得我第一次面试啥都不知道,穿的也不修边幅去应聘一家4s销售,面试官看到我,我明细感觉到了对方厌恶的神色,这是我上社会的第一课,找工作不管是什么工作,衣着得体,是 ...
- 【转载】NodeJS、NPM安装配置步骤(windows版本)
1.windows下的NodeJS安装是比较方便的(v0.6.0版本之后,支持windows native),只需要登陆官网(http://nodejs.org/),便可以看到首页的“INSTALL” ...
- [小北De编程手记] : Lesson 01 - Selenium For C# 之 环境搭建
在我看来一个自动化测试平台的构建,是一种很好的了解开发语言,单元测试框架,自动化测试驱动,设计模式等等等的途径.因此,在下选择了自动化测试的这个话题来和大家分享一下本人关于软件开发和自动化测试的认识. ...
- 【原创】.NET Core应用类型(Portable apps & Self-contained apps)
介绍 有许多种方式可以用来考虑构建应用的类型,通常类型用来描述一个特定的执行模型或者基于此的应用.举例说:控制台应用(Console Application).Web应用(Web Applicatio ...
- linux下log4j乱码解决
使用log4j的时候,在WIN系统的时候正常显示中文,但是发布到linux系统的时候中文就显示成乱码了 由于log4j配置文件中没有设置编码格式(encoding),所以log4j就使用系统默认编码. ...