Using ButterKnifeZelezny in Eclipse or ADT makes initialization of Android components easy to understand !!!!, Androideclipseadt
I. Cause
Surely those who use AndroidStudio for development and ButterKnife will have heard of the ButterKnifeZelezny component. It can generate View comments and ButterKnife annotations from the layout file in one click.
For details, see the figure:
It's so convenient !!!! One-click generation !!!! From the Bitter Sea !!!!
However, I am still not used to using AndroidStudio until now. Although it will be used, I still prefer the Eclipse Development Method for formal development projects, so I am thinking, does Eclipse have such a plug-in?
Obviously, I think too much.
But since there is no such thing, do it yourself.
Ii. Ideas
As the saying goes, "the release of the bell is still needed". Since we need to train our minds, we naturally come from ButterKnifeZelezny. In my opinion, ButterKnifeZelezny mainly involves several steps.
1. parse XML
2. Analysis Components
3. Generate the ButterKnife Annotation
So, can I use the combination of batch processing and JAVA files to create a batch file, drag the layout file into the batch processing file to parse the components in the layout file that have set the ID attribute and extract these components and IDs, finally, the string assembly technology is used to combine it into the way I want to write it into the system clipboard. Then, the user CTRL + V can paste it into the specified Activity?
I can see it!
3. Enable Consolidation
The idea is ready, and the rest is to start. The first thing to solve is the XML parsing problem.
Basically, you don't have to think too much about it. You can use DOM4J to parse it.
Create a Java Project first
Then add a tool class
LayoutUtil. java (Tool class for parsing layout files)
Package util; import java. io. file; import java. util. hashMap; import java. util. iterator; import java. util. list; import java. util. map; import org. dom4j. document; import org. dom4j. extends entexception; import org. dom4j. element; import org. dom4j. io. SAXReader; public class LayoutUtil {/*** @ author active orange * @ creation 2015-6-26 */public static Map <String, String> ParseLayout (String filePath) {Map <String, string> UnitMap = new H AshMap <String, String> (); Document doc = null; try {doc = new SAXReader (). read (new File (filePath);} catch (incluentexception e) {e. printStackTrace ();} Element root = doc. getRootElement (); if (root. attributeValue ("id ")! = Null & root. attributeValue ("id "). length ()> 0) {UnitMap. put (root. attributeValue ("id"), root. getName (); // System. out. println ("root node:" + root. getName () + ", content:" + root. attributeValue ("id");} getElement (root, UnitMap); return UnitMap;} private static void getElement (Element element, Map <String, String> map) {List list = element. elements (); // Recursive Method for (Iterator its = list. iterator (); its. hasNext ();) {Element chileE Le = (Element) its. next (); if (chileEle. attributeValue ("id ")! = Null & chileEle. attributeValue ("id "). length ()> 0) {map. put (chileEle. attributeValue ("id"), chileEle. getName (); // System. out. println ("node:" + chileEle. getName () + ", content:" + chileEle. attributeValue ("id");} getElement (chileEle, map );}}}
Since XML can be parsed, what should I do next? Of course, it is the tool class for writing concatenated strings. This class is also the core class for generating the ButterKnife annotation format. If you want to write other methods or native writing, you can modify this class.
SpellUtil. java
Package util; public class SpellUtil {/*** @ author active orange * @ creation 2015-6-26 */public static String SpellUnit (String unit, String id) {StringBuffer parseText = new StringBuffer (); id = id. replace ("@ + id/", ""); parseText. append ("@ InjectView (R. id. "+ id +") "+" \ r \ n "); parseText. append (unit + "" + id + ";"); return parseText. toString ();}}
Resolution is also available, and splicing is also available. What should we do?
Of course, it is a tool class to copy the spliced string to the clipboard.
ClipBoard. java
Package util; import java. awt. toolkit; import java. awt. datatransfer. clipboard; import java. awt. datatransfer. transferable; import java. awt. datatransfer. stringSelection; import java. awt. event. actionEvent; public class ClipBoard {/*** @ author active orange * @ creation 2015-6-26 */public static void actionreceivmed (String text) {Clipboard clipboard = Toolkit. getdefatooltoolkit (). getSystemClipboard (); // obtain the system clipboard StringSelection textInfoSelected = new StringSelection (text); // create a clipboard content instance. clipboard. setContents (textInfoSelected, null); // Add textInfoSelected to the clipboard ;}}
OK, everything is ready, and the rest is to write the bat file and call the Main method portal.
It is also very simple
CreateLayooutXml. java
Package util; import java. awt. toolkit; import java. awt. datatransfer. stringSelection; import java. util. map; public class CreateLayooutXml {/*** @ author active orange * @ creation 2015-6-26 */public static void main (String [] args) {// TODO Auto-generated method stubMap <String, String> map = LayoutUtil. parseLayout (args [0]); String parseText = ""; for (Map. entry <String, String> entry: map. entrySet () {parseText + = SpellUtil. spellUnit (entry. getValue (), entry. getKey () + "\ n";} System. out. println (parseText); ClipBoard. actionreceivmed (parseText );}}
So far, Java has finished its work. Note:
LayoutUtil.ParseLayout(args[0]);
This indicates that during compilation, this parameter must be passed in from the batch processing file. The input content is actually the absolute path of the layout file.
The rest is to write a batch file, and then pass in the file path. You can execute the Main function in CreateLayoutXml. java.
ButterKnife_Jia.bat
@ Echo offecho path: % ~ Dp0set base = % ~ Dp0set class = % base % \ binset libs = % base % \ libset class_path = % class %; % libs % \ dom4j-1.6.1.jar; @ set input = @ set/p input = drag the layout file and press Enter: java-classpath % class_path % util. createLayooutXml % input % echo % the initialization content of the layout has been copied to the clipboard @ pause
Okay, well, basically we have completed all the coding of this tool. The file engineering structure of this tool is as follows:
Check that dom4j. jar is put in lib, The. class file is put in util, And the bat file is placed in the outermost side.
Then execute the bat file.
OK, the batch processing file is executed in this step. Wait for you to input a layout file, and drag an xml file to the bat file from the layout folder in your eclipse project, press enter and you will see
As shown in, your ButterKnife annotation has been generated and is already in your clipboard. What are you waiting for? Find the corresponding Activity, ctrl + V !!!!
Below is my Ctrl + V:
@ InjectView (R. id. loc_btn)
Button loc_btn;
@ InjectView (R. id. loc_info)
TextView loc_info;
Iv. Summary
In fact, this tool does not have any technical difficulties. The key point is the idea and basic JAVA skills. I believe that after reading it, you can write many small components like this. Come on!
Resource download path:
Http://download.csdn.net/detail/jasoncol_521/8841831
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.