gpt4 book ai didi

android跨度点击事件

转载 作者:行者123 更新时间:2023-12-02 10:33:11 24 4
gpt4 key购买 nike

好的。这些都是我的问题。

  1. 我需要使用正则表达式来过滤掉除字母之外的所有内容,然后我需要将找到的单词放入 $word 标签中。有了这个 str = str.replaceAll(pattern, "$0"); 。现在我正在过滤所有正确的元素(标点符号、数字等),但它将每个单词中的每个字母封装在标签中而不是单词中。那么如何使用正则表达式将字母分组到单词中呢?

来自“(一个标签打开)t(一个关闭)(一个标签打开)h(一个关闭)(一个标签打开)i(一个关闭)(一个标签打开)s(一个关闭)(一个标签打开) i(a 关闭)(a 标签打开)s(a 关闭)(a 标签打开)w(a 关闭)(a 标签打开)r(a 关闭)(a 标签打开)o(a 关闭)(a 标签打开) n(关闭)(打开标签)g(关闭)";

至:

“(一个标签打开)这个(一个关闭)(一个标签打开)是(一个标签关闭)(一个标签打开)对(一个关闭)”;

然后我使它们可单击,我需要捕获单击事件并获取屏幕上单击的单词的位置,因为我想使用单击的事件使工具提示显示在单击的单词下方。感谢您的帮助。

公共(public)类 MainActivity 扩展 Activity {

public String text = "This is just a sentence to test you. 23 this is another number23!g?";
public TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

text = explode(text);

tv = (TextView) findViewById(R.id.tv1);

tv.setLinksClickable(true);
tv.setMovementMethod(LinkMovementMethod.getInstance());

Spanned article = Html.fromHtml(text, null, null);
setHTML(article);
}


public void setHTML(Spanned html) {
SpannableString message = new SpannableString(html.toString());
Object[] spans = html.getSpans(0, html.length(), Object.class);
for (Object span : spans) {
int start = html.getSpanStart(span);
int end = html.getSpanEnd(span);
int flags = html.getSpanFlags(span);
if (span instanceof URLSpan) {
URLSpan urlSpan = (URLSpan) span;
span = new CallbackSpan(urlSpan.getURL());
}
message.setSpan(span, start, end, flags);
}
tv.setText(message);
}
public String explode(String str){
String pattern = "([a-zA-Z])";
str = str.replaceAll(pattern, "<a href=\"$0\">$0</a>");
return str;
}

private final class CallbackSpan extends ClickableSpan {

private String m_data;
private String url_main;

public CallbackSpan(String url) {
m_data = url.substring(0);
url_main = url;

}

public void onClick(View view) {

TextView item = (TextView)findViewById(R.id.tv2);
item.setText(url_main + " was clicked.");
Log.d("item", url_main);
}
}

}

最佳答案

最新代码,请参见link form github

message.setSpan(span, start, end, flags);

在设置新范围之前,您需要删除原始范围。请看下面
The onClick() of ClickableSpan is not working for URLSpan?

编辑
您可以通过extends LinkMovementMethod捕获任何span点击事件

    import android.os.Handler;
import android.os.Message;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.text.method.MovementMethod;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.TextView;

import java.lang.ref.WeakReference;

public class LinkMovementMethodExt extends LinkMovementMethod {
public static final int LinkMovementMethod_Down = 1001;
public static final int LinkMovementMethod_Up = 2002;
private static LinkMovementMethod sInstance;
private Class mSpanClass = null;
private WeakReference<Handler> mWeakReference = null;

public static MovementMethod getInstance(Handler handler, Class spanClass) {
if (sInstance == null) {
sInstance = new LinkMovementMethodExt();
((LinkMovementMethodExt) sInstance).mWeakReference = new WeakReference<>(handler);
((LinkMovementMethodExt) sInstance).mSpanClass = spanClass;
}
return sInstance;
}

@Override
public boolean onTouchEvent(TextView widget, Spannable buffer,
MotionEvent event) {
int action = event.getAction();

if (action == MotionEvent.ACTION_UP ||
action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();

x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();

x += widget.getScrollX();
y += widget.getScrollY();

Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
/**
* get you interest span
*/
Object[] spans = buffer.getSpans(off, off, mSpanClass);
if (spans.length != 0) {
if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer, buffer.getSpanStart(spans[0]), buffer.getSpanEnd(spans[0]));
MessageSpan obj = new MessageSpan();
obj.setObj(spans);
obj.setView(widget);
Handler handler = mWeakReference.get();
if (handler != null) {
Message message = handler.obtainMessage();
message.obj = obj;
message.what = LinkMovementMethod_Down;
message.sendToTarget();
return true;
}
return false;
} else if (action == MotionEvent.ACTION_UP) {
Handler handler = mWeakReference.get();
if (handler != null) {
MessageSpan obj = new MessageSpan();
obj.setView(widget);
Message message = handler.obtainMessage();
message.obj = obj;
message.what = LinkMovementMethod_Up;
message.sendToTarget();
return true;
}
return false;
}
}
}

return super.onTouchEvent(widget, buffer, event);
}

public boolean canSelectArbitrarily() {
return true;
}

public boolean onKeyUp(TextView widget, Spannable buffer, int keyCode,
KeyEvent event) {
return false;
}

textView.setMovementMethod(LinkMovementMethodExt.getInstance());

<小时/>

编辑“android 开发者”
LinkMovementMethodExt 类添加 Handler 属性是更好的方法。
您可以捕获作为 Message 对象传递的所有 Spanned。

onTouchEvent 方法中的代码 fragment :

Message message = Handler.obtainMessage();
message.obj = buffer.getSpans(off, off, Spanned.class);//get all spanned
message.what = 111;//which value ,it is up to you
message.sendToTarget(); //send message to you target handler

您可以在处理程序类中处理预期的跨度。也许这是一种灵活的处理方式。
希望对您有帮助。

<小时/>

enter image description here


上面的textview文本是<a href='/a'>aaaa</a>123456<a href='/b'>bbbb</b>7890
我了解您的要求:
点击'aaaa',您想要得到它 s href value '/a', click 'bbbb',get it s href '/b';不触发在网络浏览器中打开的默认操作。
如果我的理解是正确的,你可以这样做:

  • 为textview等设置LinkMovementMethod:textview.setMovementMethod(LinkMovementMethodExt.getInstance(handler, URLSpan.class));
  • 获取兴趣跨度,这里是 URLSpan。
    在处理程序 handleMessage 方法中,您可以这样做:
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
int what = msg.what;
if (what == 100) {
Object[] spans = (Object[])msg.obj;
for (Object span : spans) {
if (span instanceof URLSpan) {
System.out.println(((URLSpan) span).getURL());
}
}
}
};
};
<小时/>

Download demo code

  • MainActivity 具有 color 属性,您可以根据需要指定颜色值。

怎么办?

  • 第1步,获取当前点击范围。

  • 第2步,设置当前点击范围的BackgroundColorSpan

关于android跨度点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48592989/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com