dhroid - ioc基础(@Inject*)

1 ioc即控制反转。
控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心。
控制反转还有一个名字叫做依赖注入(Dependency Injection)
dhroid的ioc不仅实现了视图和资源的注入,同时对于对象,接口的注入做了很大的努力

ioc中最简单最好实现的就是视图资源注入,dhroid的ioc核心其实是对象(接口的)注入
下面还是从简单的视图注入开始到复杂的接口对象注入
1.1注入视图

public class IocTestActivity extends BaseActivity{
@InjectView(id=R.id.text)
TextView textV;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ioc_test_activity);
if(textV==null){
Log.e("dhroid", "textV不为空,我是不会打印的");
}
}
}

 

上边就是一个视图注入的例子
textV在contentView(layout)之后就已经被赋值了

1.1注入视图
注入视图的注解是 @InjectView

@InjectView,参数id 
进行视图查找,相当于findViewById(R.id.xxx)

@InjectView 的参数属性layout
可以注入一个layout布局,

相当于LayoutInflater.from(this).inflate(R.layout.xxx, null);


如
@InjectView(layout=R.layout.ioc_head)
ViewGroup headV;


@InjectView 的参数属性inView
表明该view只某个view的子view
如 
//注入布局文件
@InjectView(layout=R.layout.ioc_head)
ViewGroup headV;
//在其他视图中查看
@InjectView(id=R.id.intext,inView="headV")
TextView childTextV;

 

表明childTextV是headV的子view
相当于 

headV=(ViewGroup )LayoutInflater.from(this).inflate(R.layout.ioc_head, null);
childTextV=headV.findViewById(R.id.intext);

 

1.1.1 为视图添加事件

下面的代码添加了点击事件

@InjectView(id=R.id.assertFile,click="toInstal")
View instalApkV;

public void toInstal(View v) {
Log.v("dhroid", "instalApk点击后我会被执行");
}

事件还有itemclick 和itemLongclick
@InjectView(id=R.id.listView,itemClick="toEditStudent",itemLongClick="toDeleteStudent")
ListView listView;

public void toEditStudent(AdapterView<?> parent, View view, int position, long id) {
Log.v("dhroid", "itemClick事件");
}

public void toDeleteStudent(AdapterView<?> parent, View view, final int position, long id) {
Log.v("dhroid", "itemLongClick事件");
}

 


1.2注入资源
使用注解@InjectResource
资源可以注入drawable,string,color,dimen

下面代码相当于getResources().getString(R.string.app_name)

//注入字串
@InjectResource(string=R.string.app_name)
String appname;

 


下面代码相当于getResources().getDrawable(R.drawable.ic_launcher)

@InjectResource(drawable=R.drawable.ic_launcher)
Drawable icDraw;

color,和dimen同理
//这里不能为int因为int有默认值0 有值的属性会被忽略,防止重复注入

@InjectResource(color=R.color.link)
Integer colorLink;

//注入图片
@InjectResource(drawable=R.drawable.ic_launcher)
Drawable icDraw;

 

1.3 注入extra
页面间数据传递也可以注入的
下面代码相当于getIntent().getStringExtra("str");

//接受传入的字符串
@InjectExtra(name="str",def="默认值")
String extra;

 

Integer,Long,Float,Double,Boolean(特殊的JSONObject,JSONArray,其他bean对象都能传)
这里需要特殊说明一下不能使用int,long,float,double因为他们有默认值,注入时发现有值会滤过的
如果有默认值请写到def属性下,
特殊的JSONObject,JSONArray和bean对象是特殊强制转换的结果,传入的对象都还string类型

传入时
it.putExtra("jo", "{name:'我是json字符串'}");
it.putExtra("array", "[{name:'我是json数组'}]");
it.putExtra("bean", "{name:'我是json字符串'}");
接受时
@InjectExtra(name="jo")
JSONObject extrajo;
@InjectExtra(name="array")
JSONArray extraarray;

@InjectExtra(name="bean")
User extrauser;

 


1.4 注入assert
@InjectAssert主要是用来注入assert中的文件的

下面代码可以注入assert文件夹下面的testtext.json的文本内容

@InjectAssert(path="testtext.json")
String testassert;

 

如果你的文本内容本来就是json可以写为

@InjectAssert(path="testtext.json")
JSONObject jo;

 


我们还能注入文件如

@InjectAssert(path="anzhi.apk")
File apkFile;

 

因为assert本身不支持文件读写的所以其实先将文件拷贝出去后然后赋值的,文件拷贝时是异步的使用时需要注意文件是否拷贝完成,

可以在需要使用前面的页面就进行一次注入(文件只会拷贝一次)

Fragment 中使用 如果你不是继承自BaseActivity,你需要在setContentView()后调用 InjectUtil.inject(this);
如果你在Fragment 中使用,需要在onActivityCreated方法中调用 InjectUtil.inject(this);
同时还可以在自定义View中使用,也是InjectUtil.inject(this);
其实BaseActivity里面很简单的


关于类继承问题
继承类时
父类中的 只有有共有属性,即public的属性才能被注入

posted @ 2015-07-16 09:59  n1rAy  阅读(642)  评论(0编辑  收藏  举报