关闭

关于android Activity中注解的使用,省去无用的findviewbyid....setonclick.....

标签: androidannotation
181人阅读 评论(0) 收藏 举报

这几天研究的一下java的反射与注解机制,又分析了开源的afinal框架发现里面用到了注解省去很多的findViewById和setonClick等方法,省去了很多的代码,而且代码一目了然,下面是我自己的心得自己的实现注解:


package com.example.anotationdemo;


import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends Activity {
@ViewInject(id = R.id.id_button, click = "doClick")
private Button mBtn;


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

//在这里使用注解绑定事件
injectView();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


private void injectView() throws IllegalAccessException,
IllegalArgumentException {
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Annotation[] annotations = field.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation instanceof ViewInject) {
int id = ((ViewInject) annotation).id();
String click = ((ViewInject) annotation).click();


field.set(this, findViewById(id));


Object object = field.get(this);


if (object instanceof View) {

//这里目前只拿setonclick为例
((View) object).setOnClickListener(new ClickImpl(click,
this));
}


}
}
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


public void doClick(View view) {
Toast.makeText(this, "toast", Toast.LENGTH_SHORT).show();
}


public class ClickImpl implements OnClickListener {
private String method;
private Activity activity;


public ClickImpl(String method, Activity activity) {
this.method = method;
this.activity = activity;
}


public void setMethod(String method) {
this.method = method;
}


public String getMethod() {
return this.method;
}


@Override
public void onClick(View v) {
try {
Method click = activity.getClass()
.getMethod(method, View.class);
click.invoke(activity, v);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

这里是注解的类:

package com.example.anotationdemo;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
 * @author 邢安栋 E-mail:1419872511@qq.com
 * @version 创建时间:2015-7-10 上午9:25:30
 */


@Retention(RetentionPolicy.RUNTIME) //表示可以通过反射获取
@Target(ElementType.FIELD)
public @interface ViewInject {
int id();


String click();
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2533次
    • 积分:93
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类