如何返回到EditText的原始背景颜色

发布于 2025-01-30 23:02:52 字数 295 浏览 2 评论 0原文

嗨,我有一个编辑文本,我想将背景图更改为原始颜色。

这样,我正在变成红色,但想返回原始颜色,我该怎么做,请

  etCostLimit.backgroundTintList = ColorStateList.valueOf(resources.getColor(
                                            R.color.newRed,
                                            null))

提前谢谢 r

Hi I have an edit text and I want to change the backgroundtint to the original color.

this way I am changing to the red colour but want to return to the original colour, how can I do that please

  etCostLimit.backgroundTintList = ColorStateList.valueOf(resources.getColor(
                                            R.color.newRed,
                                            null))

Thanks in advance
R

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

(り薆情海 2025-02-06 23:02:52

尝试了所有内容后,我终于在C#论坛上找到了答案。将其转换为Kotlin并组合其他部分,这是结果(在API 21和33上进行了验证):

将Edittext视为变量名称。

备份:

val defaultTintList =
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && editText is AppCompatEditText) {
        null //reading current value also returns null
    } else {
        //restoring editText.backgroundTintList (which is null) doesn't work correctly
        val typedValue = TypedValue()
        context.theme.resolveAttribute(R.attr.colorAccent, typedValue, true)
        ColorStateList.valueOf(ContextCompat.getColor(context, typedValue.resourceId))
    }

还原:

@SuppressLint("RestrictedApi")
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && editText is AppCompatEditText)
    (editText as AppCompatEditText).supportBackgroundTintList = defaultTintList
else
    editText.backgroundTintList = defaultTintList

After trying everything, I finally found the answer on a C# forum. Converted that to Kotlin and combining other parts, here's the result (verified on API 21 and 33):

Consider editText as the variable name.

Backup:

val defaultTintList =
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && editText is AppCompatEditText) {
        null //reading current value also returns null
    } else {
        //restoring editText.backgroundTintList (which is null) doesn't work correctly
        val typedValue = TypedValue()
        context.theme.resolveAttribute(R.attr.colorAccent, typedValue, true)
        ColorStateList.valueOf(ContextCompat.getColor(context, typedValue.resourceId))
    }

Restore:

@SuppressLint("RestrictedApi")
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP && editText is AppCompatEditText)
    (editText as AppCompatEditText).supportBackgroundTintList = defaultTintList
else
    editText.backgroundTintList = defaultTintList
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文