How to use “setTextColor(hexaValue)” on Kotlin for

2019-07-04 11:41发布

问题:

Background

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...

Very easy...

The problem

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

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

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

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

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));

However, this has multiple disadvantages:

  1. It is way longer.
  2. It converts a String, so it's much less efficient
  3. Most importantly: it works only from API 26 (Android O) , which currently is active on about 1% of Android devices worldwide.

The questions

Why does it occur?

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) ?

回答1:

Oxff000000 is resolved to Long in Kotlin so right now there is no way to use this literal as is, however 0xff000000.toInt() will give you exactly the same result as -0x1000000 so you can use .toInt() approach. Under the hood, it's the equivalent of (int)4278190080L Java cast.

Also, with Kotlin extensions you can write a simple property like that

var TextView.textColor: Long
get() {
    //... not important
}
set(value: Long) {
    this.setTextColor(value.toInt())
}

and you'll be able to use a more concise syntax textView.textColor = 0xff000000

UPDATE: As of Kotlin 1.3 it will be possible to use concise syntax like that 0xff000000u See: Jetbrains blog and the original proposal



回答2:

textView.setTextColor(Color.parseColor("#0aad3f"))


回答3:

textview.setTextColor("#ffffff".toColor())

toColor() in an extension function defined below.

fun String.toColor(): Int = Color.parseColor(this)


回答4:

You can try this to set color of your text programmatically.

textview.textColor=Color.parseColor("#22aadd")