Lightweight Android framework source code and Android source code

Source: Internet
Author: User

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 }}


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.