(1) Android uses custom annotations to initialize controls. android Initialization
Generally, we develop Android applications and use findViewById () to initialize controls. However, after a project is developed, you will find a lot of such code, which is actually repeated. At this time, you will find out how convenient the built-in Annotation (Annotation) in Java is.
1. Design a comment for ContentView
Because this annotation will be carried on the Activity class, we need to declare @ Targrt (ElementType. TYPE ).
[Java]View plaincopyprint?
- @ Target (ElementType. TYPE)
- @ Retention (RetentionPolicy. RUNTIME)
- Public @ interface ContentView {
- Value ();
- }
2. Design a comment for ContentWidget
Because this annotation is carried to the member variables of the class, we need to declare @ Targrt (ElementType. FILELD ). Class member variables specify the control objects we declare
[Java]View plaincopyprint?
- @ Target (ElementType. FIELD)
- @ Retention (RetentionPolicy. RUNTIME)
- Public @ interface ContentWidget {
- Int value ();
- }
3. Design a tool class to extract and process the information of the aforementioned custom Annotation class
[Java]View plaincopyprint?
- Public static void injectObject (Object object, Activity activity ){
- Class <?> ClassType = object. getClass ();
- // Whether the class has a ContentView Annotation
- If (classType. isAnnotationPresent (ContentView. class )){
- // Return the existing ContentView Annotation
- ContentView annotation = classType. getAnnotation (ContentView. class );
- Try {
- // Return a Method object that reflects the specified public member methods of the classes or interfaces represented by this Class object.
- Method method = classType
- . GetMethod ("setContentView", int. class );
- Method. setAccessible (true );
- Int resId = annotation. value ();
- Method. invoke (object, resId );
- } Catch (NoSuchMethodException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- } Catch (IllegalArgumentException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- } Catch (IllegalAccessException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- } Catch (InvocationTargetException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- }
- }
- // Returns an array of the Field object. These objects reflect the member variables declared by the Class object or interface,
- // Includes public, protected, default (Package) Access, and private member variables, but does not include inherited member variables.
- Field [] fields = classType. getDeclaredFields ();
- If (null! = Fields & fields. length> 0 ){
- For (Field field: fields ){
- // Whether the member variable has a ContentWidget Annotation
- If (field. isAnnotationPresent (ContentWidget. class )){
- ContentWidget annotation = field
- . GetAnnotation (ContentWidget. class );
- Int viewId = annotation. value ();
- View view = activity. findViewById (viewId );
- If (null! = View ){
- Try {
- Field. setAccessible (true );
- Field. set (object, view );
- } Catch (IllegalArgumentException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- } Catch (IllegalAccessException e ){
- // TODO Auto-generated catch block
- E. printStackTrace ();
- }
- }
- }
- }
- }
- }
4. How should we use the annotation defined in the Activity class?
[Java]View plaincopyprint?
- @ ContentView (R. layout. activity_main)
- Public class MainActivity extends Activity {
- @ ContentWidget (R. id. TV)
- Private TextView textView;
- @ Override
- Protected void onCreate (Bundle savedInstanceState ){
- Super. onCreate (savedInstanceState );
- // SetContentView (R. layout. activity_main );
- ViewUtils. injectObject (this, this );
- TextView. setText ("Custom comment ");
- }
Although there are a lot of preparations in the early stage, it will save a lot of trouble for loading layout and initializing controls during the development process in the back sequence. This is especially important for large projects, click it to cut the worker without mistake!
DEMO address
How do android custom controls input values during initialization?
If you use code, just pass the value to the constructor.
Configure and add attributes by yourself in xml, and assign values to attributes
Android control array definition and usage Problems
Bv = new Button [10];
You just created an array to store 10 buttons.
However, you have not initialized the Button.
Bv [I] = null; changed to bv [I] = new Button (this );