【发布时间】:2021-01-13 03:20:42
【问题描述】:
我想使用 setTextColor 更改选定的文本颜色字段。但是 Android Studio 给了我这个错误。我该怎么办?最小 SDK 为 21。 这是我的 CustomNumberPicker 类的代码:
import android.annotation.TargetApi
import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.NumberPicker
import android.widget.NumberPicker.OnScrollListener
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.widget.TextViewCompat
import ir.partsoftware.cup.R
import timber.log.Timber
class CustomNumberPicker : NumberPicker {
constructor(context: Context?) : super(context) {
init()
}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
init()
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
constructor(
context: Context?,
attrs: AttributeSet?,
defStyleAttr: Int,
defStyleRes: Int
) : super(context, attrs, defStyleAttr, defStyleRes) {
init()
}
private fun init() {
setDividerColor(ContextCompat.getColor(context, R.color.color_secondary))
setNumberPickerTextColor(this, ContextCompat.getColor(context, R.color.color_secondary))
this.setOnValueChangedListener { picker, oldVal, newVal ->
setNumberPickerTextColor(this, ContextCompat.getColor(context, R.color.color_secondary))
}
this.setOnScrollListener { numberPicker, scrollState ->
setNumberPickerTextColor(this, ContextCompat.getColor(context, R.color.color_secondary))
}
}
private fun setNumberPickerTextColor(numberPicker: NumberPicker, color: Int) {
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
numberPicker.textColor = color
} else {
val count = numberPicker.childCount
for (i in 0 until count) {
val child = numberPicker.getChildAt(i)
if (child is EditText) {
try {
child.setTextColor(color)
val fieldSelectorWheelPaint = numberPicker.javaClass.getDeclaredField("mSelectorWheelPaint")
val paint = fieldSelectorWheelPaint[numberPicker] as Paint
paint.color = color
fieldSelectorWheelPaint.isAccessible = true
numberPicker.invalidate()
} catch (ex: java.lang.Exception) {
// Ignore
}
}
}
}
}
private fun setDividerColor(@ColorInt color: Int) {
try {
val fDividerDrawable =
NumberPicker::class.java.getDeclaredField("mSelectionDivider")
fDividerDrawable.isAccessible = true
val d = fDividerDrawable[this] as Drawable
DrawableCompat.setTint(d, color)
d.invalidateSelf()
postInvalidate()
} catch (e: Exception) {
Timber.d(e)
}
}
override fun addView(
child: View,
index: Int,
params: ViewGroup.LayoutParams
) {
super.addView(child, index, params)
updateView(child)
}
private fun updateView(view: View) {
if (view is EditText) {
try {
TextViewCompat.setTextAppearance(view, R.style.TextAppearance_PartPay_NumPicker)
val customFont: Typeface? = ResourcesCompat.getFont(context, R.font.iran_yekan)
view.typeface = customFont
// setNumberPickerTextColor(ContextCompat.getColor(context, R.color.color_secondary))
} catch (e: Exception) {
Timber.d(e)
}
}
}
}
【问题讨论】:
-
在这个答案中检查一些解决方案:stackoverflow.com/questions/22962075/…
-
在问这个问题之前我已经检查过了
-
Tapa Save 或 Eduardo Reis 提供的解决方案不适合您吗?在低于 29 的 API 中,文本颜色只能通过反射访问。因此,解决方案是 if / else 条件,其中 if >= 29 使用 setTextColor,如果低于使用反射访问字段。
标签: android numberpicker android-number-picker