[英]Adding OnTouch and OnClickListener to the links in a TextView and changing background color of the link
I have a textview and links in it. 我有一个textview和链接。 I want the links highlighted when I touch them and also I want to open a Alertdialog when I longclick them.
我希望在触摸链接时突出显示这些链接,并且在长按它们时也想打开一个Alertdialog。 I tried to reach the solution by customizing URLSpan, but, since I can't access to mView before OnClick is called, I can't set the listeners:
我尝试通过自定义URLSpan达到解决方案,但是,由于在调用OnClick之前无法访问mView,因此无法设置侦听器:
class ConfirmSpan extends URLSpan{
View mView;
URLSpan span;
public ConfirmSpan(URLSpan span) {
super(span.getURL());
this.span = span;
//there is a nullpointerexception here since mView is null now.
mView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
((TextView)v).setBackgroundColor(0xcccccccc);
return false;
}
});
//this is also an exception
mView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.d("INFO","long click yay!");
return false;
}
});
}
//this should be called to reach the view. But then I can't handle touch state.
@Override
public void onClick(View widget) {
mView = widget;
//bla bla..
}
public void openURL() {
super.onClick(mView);
}
}
I think I must customize LinkMovementMethod class but how? 我认为我必须自定义LinkMovementMethod类,但是如何? Any comment will be appreciated.
任何意见将不胜感激。 UPDATE: I am now able to handle touch event with this code:
更新:我现在可以使用以下代码处理触摸事件:
public class CustomLinkMovementMethod extends LinkMovementMethod {
private static CustomLinkMovementMethod sInstance;
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
// Here I am changing backgorund color
if(event.getAction() == MotionEvent.ACTION_DOWN)
widget.setBackgroundColor(0xcccccccc);
if(event.getAction() == MotionEvent.ACTION_UP)
widget.setBackgroundColor(0xffffffff);
return super.onTouchEvent(widget, buffer, event);
}
public static MovementMethod getInstance() {
if (sInstance == null)
sInstance = new CustomLinkMovementMethod();
return sInstance;
}
} }
But TextView named widget, a parameter OnTouchEvent(), is not what I want. 但是,TextView命名为小部件(参数OnTouchEvent())不是我想要的。 It is all of the text.
这是全部文字。 So, when I touch the link, text becomes gray altogether.
因此,当我触摸链接时,文本将全部变为灰色。 I think I need some other methods like coloring background of the link by finding coordinates of the start and end lines of the link.
我想我需要其他一些方法,例如通过查找链接的开始和结束线的坐标来为链接的背景着色。
Thank you for asking this question, I think I may have a solution for you. 感谢您提出这个问题,我想我可能会为您提供解决方案。
Check out the code in https://gist.github.com/qtyq/90f9b4894069a8b3676c 在https://gist.github.com/qtyq/90f9b4894069a8b3676c中查看代码
It is a factory helper class for a SpannableString which allows you to easily create a SpannableString by: 它是SpannableString的工厂帮助程序类,它允许您通过以下方式轻松创建SpannableString:
Here's an example of its usage: 这是一个用法示例:
SpannableString ss = SpannableBuilder.init(bottomText)
.setColor("Terms & Conditions", getResources().getColor(R.color.orange))
.makeLink(getContext(), "Terms & Conditions", terms)
.setColor("Privacy Policy", getResources().getColor(R.color.orange))
.makeLink(getContext(), "Privacy Policy", policy)
.create();
textView1.setText(ss);
And of course you can customise the methods already in that class or add your own ones if you need further customisation, just have a read of Google's documentation for SpannableString for more information on what you can do. 当然,您可以自定义该类中的方法,或者如果需要进一步自定义,则可以添加自己的方法,只需阅读Google的SpannableString文档,以获取有关您可以执行的操作的更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.