Ich glaube, das ist nicht mit den aktuellen Sprachfunktionen und Android API möglich.
Wenn Sie in die Quelle von Anko tauchen, werden Sie feststellen, dass sie am Ende auf die triviale und etwas hässliche Lösung zurückgreifen. (Für die höheren Ebene öffentlichen Funktionen, die Sie angeblich sind zu verwenden, die sie verwenden vararg params: Pair<String, Any?>
, und dann an den folgenden delegieren.)
@JvmStatic
private fun fillIntentArguments(intent: Intent, params: Array<out Pair<String, Any?>>) {
params.forEach {
val value = it.second
when (value) {
null -> intent.putExtra(it.first, null as Serializable?)
is Int -> intent.putExtra(it.first, value)
is Long -> intent.putExtra(it.first, value)
is CharSequence -> intent.putExtra(it.first, value)
is String -> intent.putExtra(it.first, value)
is Float -> intent.putExtra(it.first, value)
is Double -> intent.putExtra(it.first, value)
is Char -> intent.putExtra(it.first, value)
is Short -> intent.putExtra(it.first, value)
is Boolean -> intent.putExtra(it.first, value)
is Serializable -> intent.putExtra(it.first, value)
is Bundle -> intent.putExtra(it.first, value)
is Parcelable -> intent.putExtra(it.first, value)
is Array<*> -> when {
value.isArrayOf<CharSequence>() -> intent.putExtra(it.first, value)
value.isArrayOf<String>() -> intent.putExtra(it.first, value)
value.isArrayOf<Parcelable>() -> intent.putExtra(it.first, value)
else -> throw AnkoException("Intent extra ${it.first} has wrong type ${value.javaClass.name}")
}
is IntArray -> intent.putExtra(it.first, value)
is LongArray -> intent.putExtra(it.first, value)
is FloatArray -> intent.putExtra(it.first, value)
is DoubleArray -> intent.putExtra(it.first, value)
is CharArray -> intent.putExtra(it.first, value)
is ShortArray -> intent.putExtra(it.first, value)
is BooleanArray -> intent.putExtra(it.first, value)
else -> throw AnkoException("Intent extra ${it.first} has wrong type ${value.javaClass.name}")
}
[email protected]
}
}
Sie die Datei finden Sie hier: https://github.com/Kotlin/anko/blob/683115f93ca6b0ed36ab36f3814dbc0da0abe7d3/anko/library/static/common/src/Internals.kt#L143
Wenn ich es richtig verstehe, Sie möchten herausfinden, welche Art von Wert sich im 'intent'' Bündel befindet? Kann man nicht einfach 'get' Methode verwenden, die' Object' zurückgibt und dann 'getClass(). GetSimpleName()' auf diesem 'Objekt' nachschaut, um die Klasse zu finden. Sie können sogar 'instanceof' verwenden, um etwas nach Typ zu machen. –