parent
cf518aeda9
commit
05405ba27e
8 changed files with 20 additions and 127 deletions
@ -1,18 +0,0 @@ |
||||
package com.project.survey.util.param; |
||||
|
||||
import androidx.annotation.Keep; |
||||
|
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
/** |
||||
* des 参数解析注解 |
||||
*/ |
||||
@Keep |
||||
@Target(ElementType.FIELD) |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
public @interface Param { |
||||
String value() default ""; |
||||
} |
@ -1,82 +0,0 @@ |
||||
package com.project.survey.util.param |
||||
|
||||
import android.app.Activity |
||||
import android.os.Bundle |
||||
import android.text.TextUtils |
||||
import androidx.fragment.app.Fragment |
||||
|
||||
/** |
||||
* 页面跳转传参 注解+反射获取页面入参 |
||||
*/ |
||||
object ParamUtil { |
||||
/** |
||||
* Fragment |
||||
*/ |
||||
fun initParam(fragment: Fragment) { |
||||
val javaClass = fragment.javaClass |
||||
fragment.arguments?.apply { |
||||
setParam(fragment, this) |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Activity |
||||
*/ |
||||
fun initParam(activity: Activity) { |
||||
activity.intent.extras?.apply { |
||||
setParam(activity, this) |
||||
} |
||||
} |
||||
|
||||
private fun setParam(obj: Any, intent: Bundle) { |
||||
val javaClass = obj.javaClass |
||||
val fields = javaClass.declaredFields |
||||
for (item in fields) { |
||||
if (item.isAnnotationPresent(Param::class.java)) { |
||||
item.getAnnotation(Param::class.java)?.let { |
||||
val key: String = if (TextUtils.isEmpty(it.value)) item.name else it.value |
||||
if (intent.containsKey(key)) { |
||||
val type = item.type |
||||
when (type) { |
||||
Boolean::class.javaPrimitiveType -> { |
||||
intent.getBoolean(key, false) |
||||
} |
||||
Int::class.javaPrimitiveType -> { |
||||
intent.getInt(key, 0) |
||||
} |
||||
Long::class.javaPrimitiveType -> { |
||||
intent.getLong(key, 0L) |
||||
} |
||||
String::class.java -> { |
||||
intent.getString(key) |
||||
} |
||||
Double::class.javaPrimitiveType -> { |
||||
intent.getDouble(key, 0.0) |
||||
} |
||||
Byte::class.javaPrimitiveType -> { |
||||
intent.getByte(key, "".toByte()) |
||||
} |
||||
Char::class.javaPrimitiveType -> { |
||||
intent.getChar(key, '\u0000') |
||||
} |
||||
Float::class.javaPrimitiveType -> { |
||||
intent.getFloat(key, 0f) |
||||
} |
||||
else -> { |
||||
intent.getParcelable(key) |
||||
} |
||||
}?.apply { |
||||
item.isAccessible = true |
||||
try { |
||||
item[obj] = this |
||||
} catch (e: IllegalAccessException) { |
||||
e.printStackTrace() |
||||
} |
||||
item.isAccessible = false |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue