我想在我的应用程序中实现一个带有芯片的自动完成编辑文本,并且我想以一种在这里完成的方式来完成它:材料设计芯片。首先,我想问一下是否有某种小部件(可能是新支持库的一部分)或我可以使用以轻松实现的解决方案。(我知道之前有人问过这个问题,但我只是想知道在此期间是否发生了一些变化)。我也找到了这个库,但我不知道如何使用它(我可以使用它)来自动完成我的数据集......以前有没有人使用过这个库并可以分享他们的经验?
任何帮助,将不胜感激!
您可以使用以下命令将芯片添加到您的布局文件中:
<com.google.android.material.chip.Chip
android:id="@+id/some_chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a chip" />
使用经典的设计支持库 28.0.0,您可以使用该包:
<android.support.design.chip.Chip
../>
您可以使用以下属性自定义组件:
android:checkable
:如果true
,芯片可以切换。如果false
,芯片就像一个按钮app:chipIcon
:用于显示芯片内的图标app:closeIcon
:用于在芯片内显示关闭图标你可以在这里找到官方文档。
我实际上最终使用了这个库。这个库提供的自动完成视图和芯片不是“实质性的”样式,但你可以自己做,不费吹灰之力。经过大量研究后,我意识到这个库为您提供了将自己的数据集实现到逻辑中的最简单方法。所有其他库都是为使用 Android 联系人或电子邮件而定制的,更改代码和查询您自己的数据集并不是那么简单。因此,如果有人想实现芯片,但使用自定义数据查询,我会说这是正确的方法。
还有一个新的材料芯片库!
到目前为止最新..这个库看起来超级简单。你需要
implementation "com.hootsuite.android:nachos:1.1.1"
和
<com.hootsuite.nachos.NachoTextView
android:id="@+id/nacho_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:chipHorizontalSpacing="2dp"
app:chipBackground="@color/chip_background"
app:chipTextColor="@color/cheddar"
app:chipTextSize="16dp"
app:chipHeight="30dp"
app:chipVerticalSpacing="3dp"/>
和
val suggestions = arrayOf("Tortilla Chips", "Melted Cheese", "Salsa", "Guacamole", "Mexico", "Jalapeno")
val adapter = ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line, suggestions)
nachoTextView.setAdapter(adapter)
玩自定义!
此链接可能会有所帮助
Chip 小部件提供了 Material Design 芯片组件的完整实现。如何在布局中包含小部件的示例代码:
<com.google.android.material.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
https://material.io/develop/android/components/chip/
另一个第三方库
Nachos 是一个用于 Android 的库,它提供了一个自定义的 TextView 允许用户在文本字段中输入文本并创建材料芯片。 https://github.com/hoosuite/nachos
MaterialChipsInput Android 的 Material Design Chips 组件的实现。该库提供两个视图:ChipsInput 和 ChipView。 https://github.com/pchmn/MaterialChipsInput
材料芯片视图。可用作类别、联系人或创建文本云的标签 https://github.com/robertlevonyan/materialChipView