Android studio setCompoundDrawables不显示图标

Android studio setCompoundDrawables不显示图标,android-studio,kotlin,android-edittext,drawable,Android Studio,Kotlin,Android Edittext,Drawable,目前,, 我正在实现一个片段来更改用户密码。为此,用户必须确认其密码。当两个密码匹配时,我想在编辑文本中显示一个图标。为了在用户键入时验证这一点,我实现了以下功能: private fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) { this.addTextChangedListener(object : TextWatcher { override fun beforeTextC

目前,, 我正在实现一个片段来更改用户密码。为此,用户必须确认其密码。当两个密码匹配时,我想在编辑文本中显示一个图标。为了在用户键入时验证这一点,我实现了以下功能:

 private fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) {
    this.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        }

        override fun afterTextChanged(editable: Editable?) {
            afterTextChanged.invoke(editable.toString())
        }
    })
}
这样我可以使用
editText.afterTextChanged{…}
来比较两个editText的值。当两个值匹配时,我当前正试图用以下代码显示图标:

val icon = ResourcesCompat.getDrawable(
        resources,
        R.drawable.ic_baseline_check_circle_24,
        null
    )

    icon?.setBounds(
        0, 0,
        icon.intrinsicWidth,
        icon.intrinsicHeight
    )
    editText.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null)
不幸的是,这不起作用。我已经尝试使用
setCompoundDrawables
而不是
setCompoundDrawablesWithIntrinsicBounds
,但是没有任何区别。此外,我尝试直接在函数中使用
R.drawable.ic\u baseline\u check\u circle\u 24
,但它也不起作用


有人知道我的实现出了什么问题吗?

在设置新的可绘制文件之前,需要覆盖以前的可绘制文件

Android文档对该功能的描述如下:

Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.

Calling this method will overwrite any Drawables previously set using setCompoundDrawablesRelative(Drawable, Drawable, Drawable, Drawable) or related methods.

因此,您需要首先将Drawable设置为null,然后重试

val icon=ResourcesCompat.getDrawable(
资源,
R.drawable.ic\u基线\u检查\u圆\u 24,
无效的
)
图标?.setBounds(
0, 0,
icon.intrinsicWidth,
icon.intrinsicHeight
)
editText.setCompoundDrawables(null,null,null,null)//添加此行代码
editText.SetCompoundDrawableSwithinInstructBounds(null,null,icon,null)

非常感谢。现在一切都正常了。