在Kotlin中不能使用argb color int值?
当我想为Kotlin中TextView
的textColor
设置动画时:
val animator = ObjectAnimator.ofInt(myTextView, "textColor", 0xFF8363FF, 0xFFC953BE)
发生此错误:
Error:(124, 43) None of the following functions can be called with the arguments supplied: public open fun <T : Any!> ofInt(target: TextView!, xProperty: Property<TextView!, Int!>!, yProperty: Property<TextView!, Int!>!, path: Path!): ObjectAnimator! defined in android.animation.ObjectAnimator public open fun <T : Any!> ofInt(target: TextView!, property: Property<TextView!, Int!>!, vararg values: Int): ObjectAnimator! defined in android.animation.ObjectAnimator public open fun ofInt(target: Any!, propertyName: String!, vararg values: Int): ObjectAnimator! defined in android.animation.ObjectAnimator public open fun ofInt(target: Any!, xPropertyName: String!, yPropertyName: String!, path: Path!): ObjectAnimator! defined in android.animation.ObjectAnimator public open fun ofInt(vararg values: Int): ValueAnimator! defined in android.animation.ObjectAnimator
似乎0xFF8363FF
和0xFFC953BE
的值不能转换为Kotlin中的Int
,但是在Java中是正常的:
ObjectAnimator animator = ObjectAnimator.ofInt(myTextView, "textColor", 0xFF8363FF, 0xFFC953BE);
有任何想法吗? 提前致谢。
0xFF8363FF
(以及0xFFC953BE
)是一个Long
,而不是一个Int
。
您必须明确地将它们转换为Int
:
val animator = ObjectAnimator.ofInt(myTextView, "textColor", 0xFF8363FF.toInt(), 0xFFC953BE.toInt())
重点是0xFFC953BE
的数值是4291384254
,所以它应该存储在一个Long
变量中。 但是这里的高位是一个符号位,表示一个负数: -3583042
,它可以存储在Int
。
这是两种语言的区别。 在Kotlin中,您应该添加-
符号来表示否定的Int
,这在Java中是不正确的:
// Kotlin print(-0x80000000) // >>> -2147483648 (fits into Int) print(0x80000000) // >>> 2147483648 (does NOT fit into Int) // Java System.out.print(-0x80000000); // >>> -2147483648 (fits into Integer) System.out.print(0x80000000); // >>> -2147483648 (fits into Integer)
- notifyItemChanged后面的RecyclerView中PopupWindow的位置错误
- Android Kotlin – 无法调用onNavigationItemSelected方法
- Kotlin:“fun main(parameters:Array <String>)”中的默认参数如何在不指定任何值的情况下打印“guest”
- Kotlin OnTouchListener调用,但不会覆盖performClick
- 如果安装了JaCoCo,Kotlin导入包级别的“val”对象将从Android库模块中失败
- SetChecked()不起作用
- 无法将列表检索到Firebase数据库
- 有没有办法在活动之间传递函数引用?
- Kotlin Android Project无法同步Gradle项目