android-仿thinkandroid 注解标签的实现

30 篇文章 0 订阅
28 篇文章 0 订阅
本文介绍了一种在Android开发中使用视图注入技术减少findViewById调用的方法。通过自定义注解和反射机制,实现了对FragmentActivity及Fragment的支持,并提供了具体实现代码。
摘要由CSDN通过智能技术生成

ThankAndroid 的标签非常方便的的解决了很多findViewById(id)的冗余代码,这个功能非常喜欢

但是开发android有时并不需要他人的框架,或者想在自己现在的项目中添加类似注解标签的功能呢

比如Fragment;FragmentActivity

代码下载地址:http://download.csdn.net/detail/b275518834/8083935

1

实现的办法就是通过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



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值