我正在使用这个库,但从他们的onClick样式来看,两者都不能处理我需要的情况。https://github.com/splitwise/TokenAutoComplete
所以我想做我自己的事。所以我有一个ContactsCompletionView
,它就是一个TextView
。并且我像这样覆盖onTouchEvent
:
override fun onTouchEvent(event: MotionEvent): Boolean {
val action = event.actionMasked
val text = text
var handled = super.onTouchEvent(event)
if (isFocused && text != null && action == MotionEvent.ACTION_UP) {
val offset = getOffsetForPosition(event.x, event.y)
if (offset != -1) {
var offseted = text.substring(offset, text.length)
var indexLeft = offseted.indexOf("(") + 1
var indexRight = offseted.indexOf(")")
if (indexLeft > 0 && indexRight > indexLeft)
Toast.makeText(context, offseted.substring(indexLeft, indexRight), Toast.LENGTH_SHORT).show()
}
}
return handled
}
这就是他们所拥有的,但我不能使用TokenImageSpan
,因为它是一个受保护的类:
@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {
int action = event.getActionMasked();
Editable text = getText();
boolean handled = false;
if (tokenClickStyle == TokenClickStyle.None) {
handled = super.onTouchEvent(event);
}
if (isFocused() && text != null && lastLayout != null && action == MotionEvent.ACTION_UP) {
int offset = getOffsetForPosition(event.getX(), event.getY());
if (offset != -1) {
TokenImageSpan[] links = text.getSpans(offset, offset, TokenImageSpan.class);
if (links.length > 0) {
links[0].onClick();
handled = true;
} else {
//We didn't click on a token, so if any are selected, we should clear that
clearSelections();
}
}
}
if (!handled && tokenClickStyle != TokenClickStyle.None) {
handled = super.onTouchEvent(event);
}
return handled;
}
我的代码可以工作,但我的问题是,每当我按下它末尾的TAG时,它就会得到下一个对象。我想这是因为我只使用:
val offset = getOffsetForPosition(event.x, event.y)
if (offset != -1) {
var offseted = text.substring(offset, text.length)
}
当他们使用:
if (offset != -1) {
TokenImageSpan[] links = text.getSpans(offset, offset, TokenImageSpan.class);
}
TokenImageSpan
扩展了ImageSpan
,所以我可以这样使用它,但我不知道如何从ImageSpan
中获取文本。你知道我该怎么修吗?
您可以使用如下可点击的span:
SpannableString ss = new SpannableString("your string comes
here");
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
//do your stuff here on click
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
};
//set click range
ss.setSpan(clickableSpan, 8, 15,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//your text view or edittext
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);
这就是我使用的:
override fun onTouchEvent(event: MotionEvent): Boolean {
val action = event.actionMasked
val text = text
var handled = super.onTouchEvent(event)
var offset = getOffsetForPosition(event.getX(), event.getY())
if (isFocused && text != null && action == MotionEvent.ACTION_UP) {
var cursor = this@ContactsCompletionView.selectionEnd
if (cursor < 0)
cursor = 0
val links: Array<ViewSpan> = text.getSpans(0, cursor, ViewSpan::class.java)
if (objects.size > 0 && objects.size >= links.size && links.size > 0 && offset < cursor)
Snackbar().make(this, objects[links.size - 1].address, com.google.android.material.snackbar.Snackbar.LENGTH_SHORT).show()
}
return handled
}
库如果ClickStyle设置为NONE,则会将光标移动到选定对象的末尾。所以我创建了一个子串,检查我有多少跨度,然后我用它作为对象的索引。以便显示光标左侧的第一个