注解简单使用

2019-03-19  本文已影响0人  沈溺_16e5

1、创建 MyAnnotationLayout 注解(和创建类一样,把 class 选成 Annotation 就可以了)

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotationLayout {
    int MyLayout() default 0;
}

2、创建 MyAnnotationField 注解

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotationField {

    int MyView() default 0;

}

3、创建 MyAnnotationMethod 注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotationMethod {

    String MyString() default "";

}

4、在 Activity 应用 MyAnnotationLayout 注解

@MyAnnotationLayout(MyLayout = R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myLayout();

    }

    private void myLayout() {

        try {
            Class aClass = Class.forName("com.example.reflect.MainActivity");

            boolean annotationPresent = aClass.isAnnotationPresent(MyAnnotationLayout.class);
            if (annotationPresent){
                MyAnnotationLayout annotation = (MyAnnotationLayout) aClass.getAnnotation(MyAnnotationLayout.class);

                int layout = annotation.MyLayout();

                Method setContentView = aClass.getMethod("setContentView", int.class);
                setContentView.invoke(this,layout);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}

5、在 Activity 应用 MyAnnotationField 注解

@MyAnnotationLayout(MyLayout = R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @MyAnnotationField(MyView = R.id.btn)
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myLayout();
        myField();

    }

    private void myField() {

        try {
            Class aClass = Class.forName("com.example.reflect.MainActivity");

            Field btn1 = aClass.getDeclaredField("btn");

            boolean annotationPresent = btn1.isAnnotationPresent(MyAnnotationField.class);
            if (annotationPresent){
                MyAnnotationField annotation = btn1.getAnnotation(MyAnnotationField.class);

                int myView = annotation.MyView();

                Method findViewById = aClass.getMethod("findViewById", int.class);
                btn = (Button)  findViewById.invoke(this, myView);

                btn.setText("fdasdasdasdasdas");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    private void myLayout() {

        try {
            Class aClass = Class.forName("com.example.reflect.MainActivity");

            boolean annotationPresent = aClass.isAnnotationPresent(MyAnnotationLayout.class);
            if (annotationPresent){
                MyAnnotationLayout annotation = (MyAnnotationLayout) aClass.getAnnotation(MyAnnotationLayout.class);

                int layout = annotation.MyLayout();

                Method setContentView = aClass.getMethod("setContentView", int.class);
                setContentView.invoke(this,layout);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}

6、在 Activity 应用 MyAnnotationMethod 注解

@MyAnnotationLayout(MyLayout = R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @MyAnnotationField(MyView = R.id.btn)
    Button btn;

    @MyAnnotationMethod(MyString = "http://www.baidu.com")
    public void get(){

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myLayout();
        myField();
        myMythod();
    }

    private void myMythod() {

        try {
            Class aClass = Class.forName("com.example.reflect.MainActivity");

            Method get = aClass.getMethod("get");

            boolean annotationPresent = get.isAnnotationPresent(MyAnnotationMethod.class);
            if (annotationPresent){
                MyAnnotationMethod annotation = get.getAnnotation(MyAnnotationMethod.class);

                String string = annotation.MyString();
                btn.setText(string);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    private void myField() {

        try {
            Class aClass = Class.forName("com.example.reflect.MainActivity");

            Field btn1 = aClass.getDeclaredField("btn");

            boolean annotationPresent = btn1.isAnnotationPresent(MyAnnotationField.class);
            if (annotationPresent){
                MyAnnotationField annotation = btn1.getAnnotation(MyAnnotationField.class);

                int myView = annotation.MyView();

                Method findViewById = aClass.getMethod("findViewById", int.class);
                btn = (Button)  findViewById.invoke(this, myView);

                btn.setText("fdasdasdasdasdas");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    private void myLayout() {

        try {
            Class aClass = Class.forName("com.example.reflect.MainActivity");

            boolean annotationPresent = aClass.isAnnotationPresent(MyAnnotationLayout.class);
            if (annotationPresent){
                MyAnnotationLayout annotation = (MyAnnotationLayout) aClass.getAnnotation(MyAnnotationLayout.class);

                int layout = annotation.MyLayout();

                Method setContentView = aClass.getMethod("setContentView", int.class);
                setContentView.invoke(this,layout);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
}
上一篇 下一篇

猜你喜欢

热点阅读