Android的一个BindView工具的实现
来源:互联网 发布:广发淘宝卡集分宝 编辑:程序博客网 时间:2024/05/29 17:47
对于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
0 0
- Android的一个BindView工具的实现
- 自己学着写一个BindView来减少findViewById的应用
- 【Android】Android的Adapter中,bindview被调用了多次
- 浅析ButterKnife的实现 (三) —— BindView
- 为什么Android的Adapter中,bindview被调用了多次
- 使用butterknife的BindView时报错@BindView not applicable to type
- 使用XmlPullParser制作BindView工具
- 使用bindview自动安装passthru驱动的调试笔记
- 一个简单blog备份工具的实现
- Java实现的一个画图工具
- 一个文本翻译工具的实现
- 实现一个简单的压测工具
- 实现Android播放声音资源的一个简单的工具类
- Appium+Robotframework实现Android应用的自动化测试-3:一个必不可少的工具介绍
- 推荐一个在线编辑Android的工具
- 一个不错的Android wifi工具类
- Android注解式绑定控件BindView
- android使用butterknife InjectView和BindView
- 跟我一起写Makefile(5)--- 清空目标文件的规则
- html表单控件select的sel.length属性
- 大牛指点绝招
- (HDU 5792)2016 Multi-University Training Contest 5 World is Exploding (逆序数、顺序数、树状数组)
- javaScript实现图片动态轮流展示
- Android的一个BindView工具的实现
- POJ3026——Borg Maze
- 数据结构实验之链表四:有序链表的归并
- Unity学习笔记 2nd —— 摄像机那些事
- Jenkins系统设置
- 瓶颈ing
- UI标签在火狐浏览器中某些问题解决
- zblog主题 www.gridsmedia.com
- 弹出窗口的两种实现方式 PopupWindow 和 Activity