简单的安卓IOC,免去findViewById

在我们开发android项目的时候,findViewById是使用最频繁且最无技术含量的代码,在比较复杂的UI结构里这将是个比较繁琐耗时的操作;那么我们有没有什么比较好的方法来规避这个操作呢?答案当时是肯定的,例如目前的框架thinkAndroid,可以通过注解的方式来完成UI控件的IOC操作。

      这里小弟利用JAVA的反射技术写了一个简单且方便的IOC小程序,下面来给大家介绍一下:

      1、android中所有的控件都会有一个ID属性,且这个属性会被注册在R.id中,那么我们就从R.id开始动手脚吧。

      

private void reflectR(){
    Class rzz = R.id.class;
    for(Field f : rzz.getDeclaredFields()){
        //Data.cacheMap为一个HashMap对象
        Data.cacheMap.put(f.getName(), f);
    }
}

以上这段代码,我们就把整个项目的R.id全都以key-value的方式存放在了一个全局的HashMap对象中。

 

      2、下面我们看看如何来实现IOC吧。

layout:test.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 
    <RelativeLayout
        android:id="@+id/titleBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
 
        <ImageView
            android:id="@+id/leftbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="false"
            android:layout_centerVertical="true"
            android:layout_gravity="left"
            android:src="@drawable/back" />
 
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
 
        <ImageView
            android:id="@+id/rightbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
 
    </RelativeLayout>
     
    <LinearLayout
        android:id="@+id/tmp1"
        android:layout_below="@id/titleBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
 
        <LinearLayout
            android:id="@+id/hj"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">
        <LinearLayout
        android:id="@+id/line1"
        android:layout_width="fill_parent"
                android:layout_height="5dp"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/by"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">
        <LinearLayout
         android:id="@+id/line2"
        android:layout_width="fill_parent"
                android:layout_height="5dp"/>
        </LinearLayout>
    </LinearLayout>
     
    <LinearLayout
        android:id="@+id/tmp2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/tmp1"
        android:orientation="horizontal" >
 
    </LinearLayout>
     
    <ListView
        android:id="@+id/listView"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_below="@id/tmp2">
    </ListView>
</RelativeLayout>

public class Test extends Activity {
    //控件变量的定义名称,必须与layout中的Id一致
    private LinearLayout hj,by,line1,line2;
    private ListView listView;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(this.getLayout());
 
        //开始IOC自动注入控件
        this.iocElement();
 
        //下借来你就可以直接使用控件了,并不需要findViewById
    }
 
    protected int getLayout() {
        return R.layout.test;
    }
 
     /**
     * 对符合自动注入规范的控件进行注入
     * 该方法的实现采取的Java反射原理
     * 免除了繁琐重复的findViewById(...)操作
     * @throws Exception
     */
    private void iocElement() throws Exception{
        Class clzz = this.getClass();
        Field[] fields = clzz.getDeclaredFields();
        for(Field f : fields){
            int v = r(d+f.getName());
            if(v!=0){
                Object obj = this.findViewById(v);
                if(obj!=null){
                    f.setAccessible(true);
                    f.set(this,obj);
                }
            }
        }
    }
 
    /**
     * 在HashMap中判断R.id中是否有该变量,有则返回改ID
     * @param fieldName
     * @return
     * @throws Exception
     */
    private static int r(String fieldName) throws Exception{
        if(Data.cacheMap.get(fieldName)!=null){
            Field f = (Field)Data.cacheMap.get(fieldName);
            return f.getInt(null);
        }else{
            return 0;
        }
    }
}

很简单的IOC程序,希望能为大家的android代码带来一点点方便!

 

以下是本人的QQ邮箱,希望可以和IT同僚互相交流,也希望能为有需求的朋友解决困难,

邮箱:490661383@qq.com


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值