如何使用"setTextColor(hexaValue)"在Android版Kotlin上, [英] How to use "setTextColor(hexaValue)" on Kotlin for Android,

查看:785
本文介绍了如何使用"setTextColor(hexaValue)"在Android版Kotlin上,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,我可以使用它的标准十六进制值直接更改TextView的文本颜色:

In Java, I could directly change the text color of a TextView, using the standard hexa-decimal value of it:

    textView.setTextColor(0xffffffff); //white
    textView.setTextColor(0x00000000); //transparent
    textView.setTextColor(0xff000000); //black
    textView.setTextColor(0xff0000ff); //blue
    //etc...

很容易...

在Kotlin上,如果我尝试编写这样的东西,则会遇到奇怪的构建错误:

On Kotlin, if I try to write such a thing, I get with a weird build error:

错误:(15,18)使用以下命令无法调用以下任何函数: 提供的参数:public open fun setTextColor(p0:ColorStateList!): android.widget.TextView中定义的单位公开打开的乐趣 setTextColor(p0:Int):android.widget.TextView中定义的单位

Error:(15, 18) None of the following functions can be called with the arguments supplied: public open fun setTextColor(p0: ColorStateList!): Unit defined in android.widget.TextView public open fun setTextColor(p0: Int): Unit defined in android.widget.TextView

我尝试过的

我试图在Internet上进行搜索,但没有看到关于十六进制值的任何特殊信息.好像在Java上一样:

What I've tried

I tried to search about this over the Internet, and I couldn't see anything special about hexa-decimal values. Seems the same like on Java:

https://kotlinlang.org/docs/reference/basic-types.html

然后,我决定只用Java编写,然后转换为Kotlin.就颜色值而言,结果是非常不可读的:

Then I decided to just write in Java, and convert to Kotlin. The result is very unreadable in terms of the color value:

    textView.setTextColor(-0x1) //white
    textView.setTextColor(0x00000000) //transparent
    textView.setTextColor(-0x1000000) //black
    textView.setTextColor(-0xffff01) //blue

在我看来,用于Kotlin的Integer的十六进制值是带符号的,而在Java上,它会自动转换为带符号的十六进制,因此这会导致值的翻转以及在需要时设置减号的必要.

To me it seem that the hexadecimal value of Integer that is used for Kotlin is signed, while on Java it's converted to signed one automatically, so this causes flipping of values and the need to set a minus sign when needed.

我唯一能想到的仍然是这样的东西:

The only thing I can think of, that still allows to read it well, is something like this:

textView.setTextColor(Integer.parseUnsignedInt("ffff0000",16));

但是,这有多个缺点:

  1. 要长得多.
  2. 它会转换字符串,因此效率要低得多
  3. 最重要的是:它仅适用于API 26(Android O),该API目前在全球约1%的Android设备上都处于活动状态.

问题

为什么会发生?

The questions

Why does it occur?

我该如何做才能使其在不进行字符串转换的情况下最易读且可以在所有Android版本(在我的情况下为minSdkVersion 14)上运行?

What exactly can I do to make it the most readable, without string conversions, and work on all Android versions (minSdkVersion 14 in my case) ?

推荐答案

很抱歉在这样一个古老的问题上提出了意见,但是扩展功能确实是最好的方法:

Sorry for putting something on such an old question, but an extension function really seemed like the nicest approach:

fun TextView.setTextColor(color: Long) = this.setTextColor(color.toInt())

现在,您可以再次访问textView.setTextColor(0xff00ff00)

now, you can just go textView.setTextColor(0xff00ff00) again

这篇关于如何使用"setTextColor(hexaValue)"在Android版Kotlin上,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆