Android基于元注解实现一个简单的字段注解

1、自定义一个注解类

package com.jingzx.anotation_pj;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Function: 自定义的注解类
 * Created by TianMing.Xiong on 2017/9/14.
 */

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
    String value() default "default value";
}

2、使用这个注解类

package com.jingzx.anotation_pj;

/**
 * Function: 使用自定义的注解
 * Created by TianMing.Xiong on 2017/9/14.
 */

public class UseMyAnnotation {
    @MyAnnotation("三小灵犀")
    private String filedName ;
}

3、解析被标注的字段的注解值

package com.jingzx.anotation_pj;

import android.util.Log;

import java.lang.reflect.Field;

/**
 * Function: 解析我的注解获取标注的值
 * Created by TianMing.Xiong on 2017/9/14.
 */

public class ParseMyAnnotation {
    public static String parseMyAnnotation(Class<?> clazz){
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field: declaredFields ) {
            if(field.isAnnotationPresent(MyAnnotation.class)){
                MyAnnotation filedName = (MyAnnotation) field.getAnnotation(MyAnnotation.class);
                Log.e("xtm", "我属性的值: "+filedName.value() );
                return filedName.value() ;
            }

        }
        return null ;
    }
}
4、测试是否成功

   xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jingzx.anotation_pj.MainActivity">
    <Button
        android:text="获取注解上的值"
        android:onClick="getAnnotationValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tv_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</LinearLayout>

MainActivity

package com.jingzx.anotation_pj;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void getAnnotationValue(View v){
        String value = ParseMyAnnotation.parseMyAnnotation(UseMyAnnotation.class);
        TextView textView = (TextView) findViewById(R.id.tv_value);
        textView.setText(value+"");
    }
}

执行效果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,我们可以通过自定义注解来修改字段的值。首先,我们需要定义一个注解。可以使用@interface关键字来定义注解。例如,假设我们要定义一个名为@CustomAnnotation的注解,该注解用于修改字段的值。 ```java import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) public @interface CustomAnnotation { String value() default ""; } ``` 在注解内部,使用了@Retention和@Target注解来指定注解的保留策略和注解作用目标。在此例中,我们设置了@Retention(RetentionPolicy.RUNTIME),表示该注解在运行时可见。@Target(ElementType.FIELD)表示该注解可以用于字段上。 接下来,我们可以将@CustomAnnotation应用到字段上,并使用反射机制获取字段并修改其值。 ```java import java.lang.reflect.Field; public class DemoClass { @CustomAnnotation("Hello World") private String value; public void setValueUsingAnnotation() throws IllegalAccessException { Field[] fields = this.getClass().getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(CustomAnnotation.class)) { CustomAnnotation annotation = field.getAnnotation(CustomAnnotation.class); field.setAccessible(true); field.set(this, annotation.value()); } } } public static void main(String[] args) throws IllegalAccessException { DemoClass demo = new DemoClass(); System.out.println("Before modification: " + demo.value); demo.setValueUsingAnnotation(); System.out.println("After modification: " + demo.value); } } ``` 在上述示例中,我们定义了一个名为DemoClass的类,并在其中声明了一个私有字段value。我们将@CustomAnnotation应用到该字段上,并在注解中设置了一个字符串值。 在setValueUsingAnnotation方法中,使用反射机制获取类的所有字段。然后,检查每个字段是否应用了@CustomAnnotation注解。如果有,则使用Field类的setAccessible方法来设置字段可访问,并使用Field类的set方法将注解中的值赋给字段。 在main方法中,我们创建了DemoClass的实例,并输出修改前后的字段值。 通过运行上述代码,我们可以看到输出结果中,字段的值在应用自定义注解后发生了变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值