工具类:
public class InjectUtils {
//(1)
public static void injectView(Activity activity){
Class<? extends Activity> cls = activity.getClass();
//获得此类的所有成员
Field[] declaredFields = cls.getDeclaredFields();
for (Field field : declaredFields) {
//判断属性是否被注解声明
if (field.isAnnotationPresent(InjectView.class)){
InjectView injectView = field.getAnnotation(InjectView.class);
//获得了注解中设置的id
int id = injectView.value();
View view = activity.findViewById(id);
//反射设置属性的值
field.setAccessible(true);//设置访问权限,允许操作private的属性
try {
field.set(activity,view);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
//(2)
public static void injectAutowired(Activity activity) {
Class<? extends Activity> cls = activity.getClass();
//获得数据
Intent intent = activity.getIntent();
Bundle extras = intent.getExtras();
if (extras == null) {
return;
}
//获得此类所有的成员
Field[] declaredFields = cls.getDeclaredFields();
for (Field field : declaredFields) {
if (field.isAnnotationPresent(Autowired.class)) {
Autowired autowired = field.getAnnotation(Autowired.class);
//获得key
String key = TextUtils.isEmpty(autowired.value()) ? field.getName() : autowired.value();
if (extras.containsKey(key)) {
Object obj = extras.get(key);
// todo Parcelable数组类型不能直接设置,其他的都可以.
//获得数组单个元素类型
Class<?> componentType = field.getType().getComponentType();
//当前属性是数组并且是 Parcelable(子类)数组
if (field.getType().isArray() &&
Parcelable.class.isAssignableFrom(componentType)) {
Object[] objs = (Object[]) obj;
//创建对应类型的数组并由objs拷贝
Object[] objects = Arrays.copyOf(objs, objs.length, (Class<? extends Object[]>) field.getType());
obj = objects;
}
field.setAccessible(true);
try {
field.set(activity, obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
工具类中的(1)
InjectView注解
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectView {
@IdRes int value();
}
使用:在Activity中使用先绑定
InjectUtils.injectView(this);
@InjectView(R.id.rv)
private RecyclerView rv;
工具类中的(2)
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Autowired {
String value() default "";
}
使用:
ArrayList<UserParcelable> userParcelableList = new ArrayList<>();
userParcelableList.add(new UserParcelable("g"));
Intent intent = new Intent(this, SecondActivity.class)
.putExtra("name", "xixi")
.putExtra("attr","h")
.putExtra("array", new int[]{1, 2, 3, 4, 5, 6})
.putExtra("userParcelable", new UserParcelable("f"))
.putExtra("userParcelables", new UserParcelable[]{new UserParcelable("a")})
.putExtra("users",new UserSerializable[]{new UserSerializable("b")})
.putExtra("strs",new String[]{"1","2"})
.putParcelableArrayListExtra("userParcelableList", userParcelableList);
startActivity(intent);
public class UserParcelable implements Parcelable {
String name;
public UserParcelable(String name) {
this.name = name;
}
protected UserParcelable(Parcel in) {
name = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<UserParcelable> CREATOR = new Creator<UserParcelable>() {
@Override
public UserParcelable createFromParcel(Parcel in) {
return new UserParcelable(in);
}
@Override
public UserParcelable[] newArray(int size) {
return new UserParcelable[size];
}
};
@Override
public String toString() {
return "UserParcelable{" +
"name='" + name + '\'' +
'}';
}
}
public class UserSerializable implements java.io.Serializable {
String name;
public UserSerializable(String name) {
this.name = name;
}
@Override
public String toString() {
return "UserSerializable{" +
"name='" + name + '\'' +
'}';
}
}
public class SecondActivity extends AppCompatActivity {
int i,j;
String k;
@Autowired
String name;
@Autowired("attr")
String attr;
@Autowired
int[] array;
@Autowired
UserParcelable userParcelable;
@Autowired
UserParcelable[] userParcelables;
@Autowired
List<UserParcelable> userParcelableList;
@Autowired("users")
UserSerializable[] userSerializables;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
InjectUtils.injectAutowired(this);
System.out.println(toString());
}
@Override
public String toString() {
return "SecondActivity{" +
"name='" + name + '\'' +
", attr='" + attr + '\'' +
", array=" + Arrays.toString(array) +
", userParcelable=" + userParcelable +
", userParcelables=" + Arrays.toString(userParcelables) +
", userParcelableList=" + userParcelableList +
", userSerializables=" + Arrays.toString(userSerializables) +
'}';
}
}
版权声明:本文为baidu_24743861原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。