在 TextInputEditText 范围中使用 ChipDrawable,如何将 clickListener 添加到 ChipDrawable CloseIcon?

Roa*_*nmo 5 android drawable kotlin android-chips android-textinputedittext

我有以下场景:

芯片定义:

<chip xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:chipIcon="@drawable/ic_baseline_house_24"
    android:text="@string/chips_holder"
    app:closeIconEnabled="true"
    />
Run Code Online (Sandbox Code Playgroud)

包含 TextInputTeditText 的 xml 部分:

...
<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
    android:enabled="true"
    android:hint="Gallery Selections"
    android:layout_weight="1">
    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/fragment_gallery_text_input_edit_text_selections"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:focusableInTouchMode="false" />
</com.google.android.material.textfield.TextInputLayout>
...

Run Code Online (Sandbox Code Playgroud)

房间 liveData 结果观察器,提供来自一组变量的数据:

...
liveDataFilterGallery?.observe(
    viewLifecycleOwner
){filterGalleryResult->
    filterGalleryResult?.let { safeFilterGalleryResult->
        safeFilterGalleryResult.apply {
            val prefixes = arrayOf(
                "Site No",
                "Farm No",
                "Site Id",
                "Assignment Id",
                "Category",
                "Time Stamp")
            fragmentGalleryTextInputEditTextSelections.setText(
                (if(siteNo.isNullOrEmpty()) "" else "${prefixes[0]} = '$siteNo';") +
                (if(farmNo.isNullOrEmpty()) "" else "${prefixes[1]} = '$farmNo';") +
                (if(siteId.isNullOrEmpty()) "" else "${prefixes[2]} = '$siteId';") +
                (if(assignmentId.isNullOrEmpty()) "" else "${prefixes[3]} = '$assignmentId';") +
                (if(category.isNullOrEmpty()) "" else "${prefixes[4]} = '$category';") +
                (if(timeStamp.isNullOrEmpty()) "" else "${prefixes[5]} = '$timeStamp';"))
            setEntryChips(fragmentGalleryTextInputEditTextSelections.text!!)
        }
    }
}
...
Run Code Online (Sandbox Code Playgroud)

setEntryChips创建芯片并用spannable此 ChipDrawable“替换”文本的函数。搜索可编辑内容;并在此基础上创建芯片。(这;是通过此处未显示的选择功能和替换功能添加的。)

private fun setEntryChips(editable: Editable)
{
    var position = 0
    Log.i(TAG, "setEntryChips: editable.length = ${editable.length}")
    Log.i(TAG, "setEntryChips: editable = '$editable'")
    if(editable.isNotEmpty()) {
        editable.split(';').mapIndexed { index, string ->
            Log.i(TAG, "setEntryChips: split='$string'")
            Log.i(TAG, "setEntryChips: index=$index")
            if(string.isEmpty()) return@mapIndexed
            val chipDrawable =
                ChipDrawable.createFromResource(requireContext(), R.xml.input_selection_chip)
            chipDrawable.text = string
            chipDrawable.setBounds(
                0,
                0,
                chipDrawable.intrinsicWidth,
                chipDrawable.intrinsicHeight
            )
            val span = ImageSpan(chipDrawable)
            editable.setSpan(
                span,
                position,
                position + string.length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
            position += string.length+1
            /*
            val chip = Chip(requireContext())
            chip.setChipDrawable(chipDrawable)
            chip.setOnCloseIconClickListener {
                Log.i(TAG, "setEntryChips: closeIcon clicked = $string")
            }
            */
            Log.i(TAG, "setEntryChips: it.length = ${string.length}")
            Log.i(TAG, "setEntryChips: position  = $position")
        }
    }
    
}
Run Code Online (Sandbox Code Playgroud)

我以编程方式将 包装chipDrawableChip视图中的建议失败了,因为芯片从 中消失TextInputEditText,因此不是有效的解决方案。

如何clickListenercloseIconthen 添加 a 并使芯片在TextInputEditText视图中可见?

RG