问题描述
我有以下场景:
芯片定义:
<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"
/>
包含 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>
...
一个房间 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!!)
}
}
}
...
一个函数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,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")
}
}
}
我建议以编程方式将 chipDrawable
包装到 Chip
视图中失败,因为芯片从 TextInputEditText
中消失,因此不是有效的解决方案。
然后如何将 clickListener
添加到 closeIcon
并保持芯片在 TextInputEditText
视图中可见?
RG
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)