Android 利用反射和注解获取Activity之间的传值
1、首先创建注解类:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowire {
String value() default "";
}
2、编写通过反射拿去属性值,以及设置值到指定属性上:
public class InitExtureValueUtil {
public static void getExtureValue(Activity activity) {
if (activity == null) {
return;
}
// 获取类信息
Class<? extends Context> aClass = activity.getClass();
// 获取属性
Field[] declaredFields = aClass.getDeclaredFields();
if (declaredFields == null || declaredFields.length == 0) {
return;
}
// 获取intent
Method getIntent;
// 可以直接用后面这个获取,也可以用下面的反射获取 intent = activity.getIntent()
Intent intent;
try {
getIntent = aClass.getMethod("getIntent");
intent = (Intent) getIntent.invoke(activity);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
return;
}
if (getIntent == null || intent == null) {
return;
}
for (Field field : declaredFields) {
// 获取注解
Autowire annotation = field.getAnnotation(Autowire.class);
if (annotation != null) {
String key = annotation.value();
if (TextUtils.isEmpty(key)) {
// 如果注解没有设置key,则设置字段的名称为key
key = field.getName();
}
// 获取到传递过来的值 这种获取到的数据不用判断类型,后面直接赋值给属性即可
Object value = intent.getExtras().get(key);
if (value == null) {
continue;
}
// 设置属性可改变(私有属性才需要进行设置,共有属性设置了也没关系)
field.setAccessible(true);
// 设置值到当前页面的属性上面
try {
field.set(activity, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
3、创建数据类型1:
public class GoodBean implements Serializable {
private static long serialVersionUID = 1L;
private String name;
private int size;
private String productDate;
public GoodBean(String name,int size,String productDate){
this.name = name;
this.size = size;
this.productDate = productDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getProductDate() {
return productDate;
}
public void setProductDate(String productDate) {
this.productDate = productDate;
}
@Override
public String toString() {
return "GoodBean{" +
"name='" + name + '\'' +
", size=" + size +
", productDate='" + productDate + '\'' +
'}';
}
}
4、创建数据类型2:
public class GoodBeanParc implements Parcelable {
private String name;
private int age;
public GoodBeanParc(String name,int age){
this.name = name;
this.age = age;
}
protected GoodBeanParc(Parcel in) {
name = in.readString();
age = in.readInt();
}
public static final Creator<GoodBeanParc> CREATOR = new Creator<GoodBeanParc>() {
@Override
public GoodBeanParc createFromParcel(Parcel in) {
return new GoodBeanParc(in);
}
@Override
public GoodBeanParc[] newArray(int size) {
return new GoodBeanParc[size];
}
};
@Override
public String toString() {
return "GoodBeanParc{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
}
5、起始页面:
public class MainActivity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findViewById(R.id.gotoNextPage).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent inttool = new Intent(MainActivity2.this,MainActivityTwo.class);
inttool.putExtra("name", "王大锤");
inttool.putExtra("age", 18);
inttool.putExtra("good", new GoodBean("苹果", 1000, "2021年4月20日14:12:26"));
inttool.putExtra("goodParc",new GoodBeanParc("21",1));
startActivity(inttool);
}
});
}
}
6、结束页面:
public class MainActivityTwo extends AppCompatActivity {
private static final String TAG = "InitExtureValueUtil";
@Autowire
private String name;
@Autowire
private int age;
@Autowire
private GoodBean good;
@Autowire
private GoodBeanParc goodParc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_two);
InitExtureValueUtil.getExtureValue(this);
Log.e(TAG,"name==" + name);
Log.e(TAG,"age==" + age);
Log.e(TAG,"good==" + good);
Log.e(TAG,"goodParc==" + goodParc);
}
}
7、结束页面打印的值
2021-04-22 15:41:21.674 13387-13387/com.xhs.testapplication E/InitExtureValueUtil: name==王大锤
2021-04-22 15:41:21.675 13387-13387/com.xhs.testapplication E/InitExtureValueUtil: age==18
2021-04-22 15:41:21.675 13387-13387/com.xhs.testapplication E/InitExtureValueUtil: good==GoodBean{name='苹果', size=1000, productDate='2021年4月20日14:12:26'}
2021-04-22 15:41:21.675 13387-13387/com.xhs.testapplication E/InitExtureValueUtil: goodParc==GoodBeanParc{name='21', age=1}