Android的一個BindView工具的實現

對於Android已經有很多Bind View的工具了,大多都是使用了反射和註解的方法。那麼如何實現一個簡易的代碼生成工具呢?其實不難,只要會寫代碼都可以試試。這些天,我試了一下,並將它做成了工具。代碼如下:

package com.example;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by owant on 2016/8/4.
 */
public class BindViewTool {

    public static String xmlPath = "F:\\MyWorkAS\\NewHome\\app\\src\\main\\res\\layout\\activity_main.xml";
    //第一個是類型,第二個是名字
    public static String formatValue = "private {0} {1};";
    //第一個是變量,第二是類型,第三個是ID
    public static String formatBind = "{0}=({1})findViewById(R.id.{2});";

    public static void main(String[] arg) {
        //讀取文件
        try {

            String path="";
            path=arg[0];
            if(path.length()<0){
                System.out.println("請輸入文件路徑");
                return;
            }

            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File(path)));

            StringBuffer context = new StringBuffer();
            String line;
            String startLine = "";
            String endLine = "";

            while ((line = bufferedReader.readLine()) != null) {
                if (line.trim().length() > 0) {
                    //進入<
                    if (line.trim().startsWith("<") && !line.trim().startsWith("</")) {
                        startLine = line.trim();
                    }

                    //如果已經進入了<,進行查找Id
                    if (startLine.length() > 0) {
                        if (line.indexOf("android:id=\"@+id/") > 0) {
                            endLine = line.trim();
                            context.append(startLine.trim()).append("&&");
                            context.append(endLine.trim()).append("&&");

                            startLine = "";
                            endLine = "";
                        }
                    }
                }
            }

            String contextValue = context.toString();

            contextValue = contextValue.substring(0, contextValue.length() - "&&".length());

            String[] splits = contextValue.split("&&");

            int length = splits.length;
            List<String> variables = new ArrayList<String>();
            List<String> binds = new ArrayList<String>();

            for (int i = 0; i < length; i = i + 2) {
                String type = splits[i].replace("<", "");

                if (type.lastIndexOf(".") > 0) {
                    type = type.substring(type.lastIndexOf(".")+1, type.length());
                }

                String id = splits[i + 1].substring("android:id=\"@+id/".length(), splits[i + 1].length() - 1);
                // public static String formatValue = "private {0} {1}";
                String value = formatValue.replace("{0}", type);
                value = value.replace("{1}", id);
                variables.add(value);

                //public static String formatBind = "{0}=({1})findViewById(R.id.{2})";
                String bind = formatBind.replace("{0}", id);
                bind = bind.replace("{1}", type);
                bind = bind.replace("{2}", id);
                binds.add(bind);
            }


            System.out.println();
            System.out.println();

            for (String var : variables) {
                System.out.println(var);
            }

            System.out.println();
            System.out.println();

            for (String b : binds) {
                System.out.println(b);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

簡單分析一下,就是進行一下的判斷,就是在AndroidStudio格式化代碼後,進行這樣的分析:

1.讀取文件的一行;
2.如果是”<”開始的進行獲取(A);
3.如果是有”android:id=”@+id/”,進行獲取(A)
4.之後進行(A)的出來,如果進入”<”並且下一行時”android:id=”@+id/”,那麼就是一個Bind View的必要條件
5.之後整理

最後的工具生成後運行效果如下:

對於如何生存jar大家可以參考我的上一篇文章進行操作。
下載路徑 http://download.csdn.net/detail/u012131702/9597057

發佈了46 篇原創文章 · 獲贊 19 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章