Lightweight Android framework source code and Android source code
Your own Activity package com. young. android; import com. young. android. annotation. viewInject; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; import android. widget. toast; public class MainActivity extends YoungActivity {@ ViewInject (id = R. id. TV) private TextView textView; @ ViewInject (id = R. id. but) private Button button; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); textView. setText ("yong inject"); button. setText ("annotation but"); button. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubToast. makeText (getApplicationContext (), "injection", 1 ). show ();}});}}
<Pre name = "code" class = "html"> laear_width = "match_parent" android: layout_height = "match_parent" android: layout_height = "match_parent" android: orientation = "vertical"> <TextView android: layout_width = "fill_parent" android: textSize = "20sp" android: layout_height = "wrap_content" android: id = "@ + id/TV"/> <Button android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: id = "@ + id/but"/> </LinearLayout>
Parent Activitypackage com. young. android; import java. lang. reflect. field; import com. young. android. annotation. viewInject; import com. young. android. exception. illegalParameterException; import android. app. activity; import android. view. view; public class YoungActivity extends Activity {@ Overridepublic void setContentView (int layoutResID) {// TODO Auto-generated method stubsuper. setContentView (layoutResID); Class <? Extends YoungActivity> clz = getClass (); Field [] fs = clz. getDeclaredFields (); for (Field f: fs) {ViewInject viewInject = f. getAnnotation (ViewInject. class); if (viewInject! = Null) {int id = viewInject. id (); View v = findViewById (id); if (v = null) throw new IllegalParameterException ("@ ViewInject parameter is invalid, you should use this example: define TextView in the corresponding layout file with the id TV, and Mark @ ViewInject (id = R. id. TV) "); f. setAccessible (true); try {f. set (YoungActivity. this, v);} catch (IllegalAccessException e) {// TODO Auto-generated catch block // e. printStackTrace (); throw new IllegalParameterException ("@ ViewInject parameter is invalid. You should use this example: first define TextView in the corresponding layout file, id is TV, then Mark @ ViewInject (id = R. id. TV) ");} catch (IllegalArgumentException e) {// TODO Auto-generated catch block // e. printStackTrace (); throw new IllegalParameterException ("@ ViewInject parameter is invalid. You should use this example: first define TextView in the corresponding layout file, id is TV, then Mark @ ViewInject (id = R. id. TV) ");} f. setAccessible (false );}}}}
Annotation defines package com. young. android. annotation; import java. lang. annotation. elementType; import java. lang. annotation. retention; import java. lang. annotation. retentionPolicy; import java. lang. annotation. target; @ Target (ElementType. FIELD) @ Retention (RetentionPolicy. RUNTIME) public @ interface ViewInject {public int id () default 0 ;}
Abnormal package com. young. android. exception; public class IllegalParameterException extends RuntimeException {private static final long serialVersionUID = response; public IllegalParameterException (String detailMessage) {super (detailMessage); // TODO Auto-generated constructor stub }}