如何在 android textview 中实现类似 stackoverflow 的标签?

How to implement a stackoverflow like tag in android textview?

我正在开发一个食品订购应用程序,用户 select 从列表视图中选择所需的配料。

我希望 selected 项目像流行标签时尚一样显示在 Whosebug 中

在图片中,我在 Whosebug 应用程序中 selected java,C# 和 android,它们显示为独立的盒装项目。

我想在没有 X 的情况下实现相同的字符串框。

我有一个文本视图,我尝试将它们添加为独立项目,但不确定如何实现。

任何帮助都会非常有用。

使用 Chip 构建的最佳方式,请查看下面的一些代码片段。

在您的 .xml

中添加以下代码
<com.google.android.material.chip.ChipGroup
    android:id="@+id/filter_chip_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/filter_chip_txt">

    <com.google.android.material.chip.Chip
        android:id="@+id/filter_chip"
        style="@style/Widget.MaterialComponents.Chip.Filter"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:layout_marginStart="10dp"
        android:text="Dogs"/>

    <com.google.android.material.chip.Chip
        android:id="@+id/filter_chip2"
        style="@style/Widget.MaterialComponents.Chip.Filter"
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="Cats"/>
</com.google.android.material.chip.ChipGroup>

在你的.java

里面
 ChipGroup filterChipGroup = findViewById(R.id.filter_chip_group);
    Chip filterChip = findViewById(R.id.filter_chip);
    Chip filterChip2 = findViewById(R.id.filter_chip2);
    CompoundButton.OnCheckedChangeListener filterChipListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Log.i(TAG, buttonView.getId() + "");
        }
    };
    filterChip.setOnCheckedChangeListener(filterChipListener);
    filterChip2.setOnCheckedChangeListener(filterChipListener);

我希望这有助于实现您的期望。