When we developed the Android project, Findviewbyid was the most frequent and most technical-free code, which would be a tedious and time-consuming operation in a more complex UI structure, so what better way to circumvent the operation? The answer was affirmative, such as the current framework thinkandroid, which can be annotated to complete the IOC operation of the UI control.
Here the younger brother uses the Java reflection technology to write a simple and convenient IOC applet, below to introduce you:
1, all the controls in Android will have an id attribute, and this property will be registered in R.id, then we start from R.id.
private void Reflectr () {Class rzz = r.id.class;for (Field f:rzz.getdeclaredfields ()) { // Data.cachemap is a HashMap object Data.cacheMap.put (F.getname (), f);}}
The above code, we put the entire project r.id all in a key-value way in a global HashMap object.
2. Let's take a look at how to implement the 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 "> <Relativ Elayout android:id= "@+id/titlebar" android:layout_width= "fill_parent" android:layout_height= "Wrap_con Tent "><imageview android:id=" @+id/leftbutton "android:layout_width=" wrap_content "android:layout_height=" WR Ap_content "android:layout_alignparentleft=" true "android:layout_centerinparent=" false "Android:layout_centervert Ical= "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 Oid:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal" > <linearlayout android:id= "@+id/hj" android:layout_width= "Fill_parent" Android:layo ut_height= "Wrap_content" android:layout_weight= "1" android:orientation= "vertical" > <linearl Ayout 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= "vertic Al "><linearlayout android:id=" @+id/line2 "android:layout_width=" Fill_parent "android:layout_height=" 5 DP "/> </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_h eight= "Fill_parent" android:layout_width= "fill_parent" android:layout_below= "@id/tmp2" > </ListView& Gt;</relativelayout>
public class Test extends Activity {the definition name of the//control variable must be the same as the ID in LayoutPrivate LinearLayout hj,by,line1,line2;private listview listview; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (This.getlayout ());//Start IOC auto-injection controlThis.iocelement ();//Under loan you can use the control directly, do not need Findviewbyid}protected int GetLayout () {return r.layout.test;}/** * Inject controls that conform to the automatic injection specification * The implementation of this method takes the Java reflection principle * exempt from tedious repetition of Findviewbyid (...) Operation * @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.findvi Ewbyid (v); if (obj!=null) {f.setaccessible (true); F.set (this,obj);}}}/** * In HashMap to determine whether the variable in r.id, there is the change 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;}}}
Very simple IOC program, hoping to bring a little bit of convenience to everyone's Android code!
The following is my QQ mailbox, I hope to communicate with it colleagues, but also hope to have the needs of friends to solve the difficulties,
Email: [Email protected]
Simple Android IOC, eliminate Findviewbyid