【发布时间】:2014-07-08 12:27:57
【问题描述】:
我使用this link 和this project link 来创建TagTextViews。
我稍微修改了一下,因为每个项目只需要一个字符串,结果如下:
在链接的代码中有一个ClickableSpan 和一个删除“标签”的onClick。因此,如果您单击“标签”上的任意位置,它将删除此标签项。
我想要的是:
- 仅在单击 X(TextView 的
android:drawableRight)时删除“标签”。 - 如果项目本身被点击,请执行其他操作。
下面是链接中与我的问题相关的部分代码:
custom_textview.xml:
<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/et_textview"
...
android:drawableRight="@android:drawable/ic_menu_close_clear_cancel"
... />
在 TagTextView.java 中:
BitmapDrawable bmpDrawable = getBitmapFromText(userInputString);
bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(), bmpDrawable.getIntrinsicHeight());
ssb.setSpan(new ImageSpan(bmpDrawable), spanEnd, startIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
this.setMovementMethod(LinkMovementMethod.getInstance());
ClickableSpan clickSpan = new ClickableSpan() {
@Override
public void onClick(View view) {
// TODO: If the tiny X is clicked on the Item:
deleteString();
// TODO: else (If the item itself (so not the tiny X) is clicked):
// Do something else
}
};
ssb.setSpan(clickSpan, spanEnd, startIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
我确实找到了使用OnTouchListener 的this SO-question。但由于我正在处理多个列表项,并且在 Android 中没有 OnItemTouchListener,我不知道如何在 ClickableSpan#onClick() 中正确实现它。
编辑:
好吧,事实证明我犯了一个非常愚蠢的错误。 getBitmapFromText(String) 从具有 android:drawableRight 的 TextView 创建单个位图。所以代码中使用的ImageSpan(bitmapDrawable)是整个'Tag'。
由于现有代码在退格时也会删除标签,因此我现在将其用作删除功能,并且当我单击“标签”时执行我自己的方法。此外,我还删除了android:drawableRight,以防止我的应用用户混淆。
TL;DR:我将其更改为:单击“标记”= 做我想做的事;退格“标签”=删除方法。
很抱歉这可能造成的混乱,因为我没有包含整个代码。
【问题讨论】:
标签: android android-layout android-drawable touch-event onitemclicklistener