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==182021-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}
总结
以上是内存溢出为你收集整理的Android 利用反射和注解获取Activity之间的传值全部内容,希望文章能够帮你解决Android 利用反射和注解获取Activity之间的传值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)