The use of Butterknifezelezny,android component initialization in Eclipse or ADT has become easy to understand!!!!

Source: Internet
Author: User

First, causes

presumably using Androidstudio to develop and use Butterknife's friends must have heard of butterknifezelezny this component, it can be generated from the layout file in a single click on the View declaration and Butterknife Annotations.


For more information, see figure:


It's so convenient!!! A key to generate AH there are wood!!! Out of the misery AH there are wood!!!

But wait, I still am not accustomed to use Androidstudio, although will use but formal development project, still prefer eclipse development way, so I think, Eclipse has such plug-in?

Obviously, I think more.

But since it's not, do it yourself.


Second, the idea


As the saying goes, "The bell must be Bell people", since to train of thought, nature is from Butterknifezelezny, in my opinion butterknifezelezny is mainly a few steps

1. Parsing xml

2. Analysis components

3. Generate Butterknife Annotations

So, can I use batch processing and Java files in a combination of a batch file, as long as the layout file into the batch file will be able to resolve the layout file in which the components set the id attribute, and the components and IDs extracted, and finally the use of string assembly technology to combine it into my writing , and then write to the system's sticky board, and finally the user Ctrl + V to paste into the specified activity?

I think it's OK!


Iii. Opening and finishing

The idea has already, the rest is to begin to do, the first of course to solve is the XML parsing problem.

Basically do not think too much, the use of dom4j analysis can be.

Build a Java Project first

Then add the 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.documentexception;import Org.dom4j.Element;import Org.dom4j.io.saxreader;public class Layoututil {/*** @author Vitality Orange * @creation 2015-6-26*/public static map<string,  String> parselayout (String filePath) {map<string,string> unitmap=new hashmap<string,string> ();          Document doc = null;          try {doc = new Saxreader (). Read (new File (FilePath));          } catch (Documentexception e) {e.printstacktrace ();         } Element root = Doc.getrootelement (); if (Root.attributevalue ("id")!=null&&root.attributevalue ("id"). Length () >0) {Unitmap.put (Root.getnam              E (), Root.attributevalue ("id"));       }//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 Chileele = (Element) Its.next ();                    if (Chileele.attributevalue ("id")!=null&&chileele.attributevalue ("id"). Length () >0) {                Map.put (Chileele.getname (), Chileele.attributevalue ("id"));              }//System.out.println ("Node:" +chileele.getname () + ", Content:" +chileele.attributevalue ("id"));          GetElement (CHILEELE,MAP); }      }   }



Now that you can parse the XML, what do you do next? Of course it is the tool class that writes the concatenation string, this class is also the core class to generate the Butterknife annotation format, if you want to do other ways of writing or the original wording, modify this class can be.


Spellutil.java

Package Util;public class Spellutil {/*** @author Vitality Orange * @creation 2015-6-26*/public static string Spellunit (String unit,str ing ID) {stringbuffer parsetext=new stringbuffer (); Id=id.replace ("@+id/", "");p Arsetext.append ("@InjectView (r.id.") +id+ ")" + "\ r \ n");p Arsetext.append (unit+ "" +id+ ";"); return parsetext.tostring ();}}


Analysis also has, splicing also has, what is left to do?

Of course, it is to copy a good string copied to the sticker board, or a tool class.

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 Vitality Orange * @creation 2015-6-26*/public static void Actio Nperformed (String text) {   Clipboard Clipboard = Toolkit.getdefaulttoolkit (). Getsystemclipboard ();//Get System Pasteboard   StringSelection textinfoselected = new StringSelection (text); Create a Clipboard content instance.   Clipboard.setcontents (textinfoselected, NULL); Add the textinfoselected to the Pasteboard;}   }


OK, everything is ready, and the rest is to write the bat file and invoke the Main method entry.

It's very simple.

Createlayooutxml.java

<span style= "FONT-SIZE:18PX;" >package util;import java.awt.toolkit;import Java.awt.datatransfer.stringselection;import java.util.Map;public Class Createlayooutxml {/*** @author Vitality Orange * @creation 2015-6-26*/public static void Main (string[] args) {//TODO Auto-genera Ted Method Stubmap<string,string> Map=layoututil.parselayout (Args[0]); String parsetext= ""; for (map.entry<string, string> entry:map.entrySet ()) {         ParseText + = Spellutil.spellunit (Entry.getkey (), Entry.getvalue ()) + "\ n";}   System.out.println (ParseText); Clipboard.actionperformed (ParseText);}} </span>
so far, the Java thing has done, note:
<span style= "FONT-SIZE:18PX;" >layoututil.parselayout (Args[0]);</span>
<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > The meaning of this sentence is that when compiling, this parameter needs to be passed from the batch file, the incoming content is actually the path of the layout file </span>

The rest of the matter is to write a batch file, and then you can 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 into the layout file and enter: Java-classpath%class_path% util. Createlayooutxml%input%echo% The initialization content of the layout has been copied to the sticky board @pause
Well, well, basically we've done all the coding work for this tool, the file engineering structure of this tool is this:



Look, Lib is put in Dom4j.jar,util is placed in the. class file, the bat file is placed on the outermost.

Then start executing the bat file


OK, the batch file executes to this step, waiting for you to pass in a layout file, simply drag an XML into the bat file from the layout folder in your Eclipse project, and press ENTER to do so, then you will see


As shown, your butterknife's notation has been generated, and already in your sticky board, and so what, to find the corresponding ACTIVITY,CTRL+V bar!!!

Below is my Ctrl + V-OH:

@InjectView (R.ID.LOC_BTN)
Button loc_btn;
@InjectView (R.id.loc_info)
TextView Loc_info;

Iv. Summary

In fact, this gadget has no technical difficulties, the key place is the idea and Java basic skills, I believe that after reading, you can also write a lot of similar small components, refueling!


Resource Download Path:

http://download.csdn.net/detail/jasoncol_521/8841311


The use of Butterknifezelezny,android component initialization in Eclipse or ADT has become easy to understand!!!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.