ThankAndroid 的标签非常方便的的解决了很多findViewById(id)的冗余代码,这个功能非常喜欢
但是开发android有时并不需要他人的框架,或者想在自己现在的项目中添加类似注解标签的功能呢
比如Fragment;FragmentActivity
代码下载地址:http://download.csdn.net/detail/b275518834/8083935
实现的办法就是通过java反射 读取注解标签,以下是核心代码
package com.example.zzzzttt; import java.lang.annotation.Annotation; import test.TAInjectView; import test.UtilAnnotation; import test.UtilAnnotation.AnnotationField; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.text.Layout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.TextView; public class FullscreenActivity extends FragmentActivity { @TAInjectView(id=R.id.text1 , key="R.id.text1") public TextView text1; @TAInjectView(id=R.id.text2 , key="R.id.text2") public TextView text2; @TAInjectView(id=R.id.text3 , key="R.id.text3") public TextView text3; @Override protected void onCreate(Bundle savedInstanceState) { this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 super.onCreate(savedInstanceState); setContentView(R.layout.activity_fullscreen); UtilReadMark.read(this, getWindow().getDecorView());//读取指定View 并读取注解 AnnotationField<TAInjectView> annField1=UtilAnnotation.readAnnotationByFieldName(FullscreenActivity.class, TAInjectView.class, "text1"); text1.setText(annField1.getAnnotation().key()); AnnotationField<TAInjectView> annField2=UtilAnnotation.readAnnotationByFieldName(FullscreenActivity.class, TAInjectView.class, "text2"); text2.setText(annField2.getAnnotation().key()); AnnotationField<TAInjectView> annField3=UtilAnnotation.readAnnotationByFieldName(FullscreenActivity.class, TAInjectView.class, "text3"); text3.setText(annField3.getAnnotation().key()); getSupportFragmentManager() .beginTransaction() .add(R.id.layout, new TestFragment()) .commit(); } public class TestFragment extends Fragment{ @TAInjectView(id=R.id.text1 , key="R.id.text4") public TextView text1; @TAInjectView(id=R.id.text2 , key="R.id.text5") public TextView text2; @TAInjectView(id=R.id.text3 , key="R.id.text6") public TextView text3; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View root = inflater.inflate(R.layout.activity_fragment, null); UtilReadMark.read(this, root); AnnotationField<TAInjectView> annField1=UtilAnnotation.readAnnotationByFieldName(this.getClass(), TAInjectView.class, "text1"); text1.setText(annField1.getAnnotation().key()); AnnotationField<TAInjectView> annField2=UtilAnnotation.readAnnotationByFieldName(this.getClass(), TAInjectView.class, "text2"); text2.setText(annField2.getAnnotation().key()); AnnotationField<TAInjectView> annField3=UtilAnnotation.readAnnotationByFieldName(this.getClass(), TAInjectView.class, "text3"); text3.setText(annField3.getAnnotation().key()); return root; } } }
<pre name="code" class="java"> /*** * 读取指定specifyClass类的annottationClass标签 * * @param specifyClass 指定需要读取的类 * @param annottationClass 指定标签类 * @return 标签类数组 */ @SuppressWarnings("unchecked") public static <A extends Annotation, T extends AnnotationField<A>> AnnotationField<A>[] readAnnotationsByField(Class<?> specifyClass, Class<? extends Annotation> annottationClass) { Field[] fields = specifyClass.getDeclaredFields(); //获得specifyClass所有字段 ArrayList<AnnotationField<A>> list = new ArrayList<AnnotationField<A>>(); int length = 0; for (Field field : fields) { if (field.isAnnotationPresent(annottationClass)) { //判断是否包含annottationClass类注解标签 A ann = (A) field.getAnnotation(annottationClass); //获得annottationClass类注解标签 length++; AnnotationField<A> annField = new AnnotationField<A>(ann, field); list.add(annField); //添加到list种 } } return list.toArray((AnnotationField<A>[]) Array.newInstance(AnnotationField.class,length));//将list转化为数组返回 }
代码下载地址:http://download.csdn.net/detail/b275518834/8083935