Android 将OnTouch和OnClickListener添加到TextView中的链接并更改链接的背景色
我有一个文本视图和链接。我希望在触摸链接时突出显示链接,并且在单击链接时打开Alertdialog。我试图通过自定义URLSpan来获得解决方案,但是,由于在调用OnClick之前我无法访问mView,因此我无法设置侦听器:Android 将OnTouch和OnClickListener添加到TextView中的链接并更改链接的背景色,android,textview,linkmovementmethod,Android,Textview,Linkmovementmethod,我有一个文本视图和链接。我希望在触摸链接时突出显示链接,并且在单击链接时打开Alertdialog。我试图通过自定义URLSpan来获得解决方案,但是,由于在调用OnClick之前我无法访问mView,因此我无法设置侦听器: class ConfirmSpan extends URLSpan{ View mView; URLSpan span; public ConfirmSpan(URLSpan span) { super(span.getURL())
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);
}
}
我想我必须自定义LinkMovementMethod类,但如何自定义呢?
如有任何意见,将不胜感激。
更新:
我现在可以使用以下代码处理触摸事件:
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;
}
}
但是名为widget的TextView,一个参数OnTouchEvent(),不是我想要的。这是所有的文本。因此,当我触摸链接时,文本完全变成灰色。我想我还需要一些其他的方法,比如通过查找链接开始线和结束线的坐标来给链接的背景上色。谢谢你提出这个问题,我想我可能有一个解决方案给你 查看中的代码 它是一个用于SpannableString的factory helper类,允许您通过以下方式轻松创建SpannableString:
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);
当然,您可以自定义该类中已有的方法,或者添加您自己的方法,如果您需要进一步自定义,只需阅读Google的SpannableString文档,了解有关您可以做什么的更多信息。谢谢您提出这个问题,我想我可能会为您提供一个解决方案 查看中的代码 它是一个用于SpannableString的factory helper类,允许您通过以下方式轻松创建SpannableString:
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);
当然,您可以自定义该类中已有的方法,或者添加您自己的方法,如果您需要进一步自定义,只需阅读Google的SpannableString文档,了解有关您可以做什么的更多信息。谢谢您提出这个问题,我想我可能会为您提供一个解决方案 查看中的代码 它是一个用于SpannableString的factory helper类,允许您通过以下方式轻松创建SpannableString:
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);
当然,您可以自定义该类中已有的方法,或者添加您自己的方法,如果您需要进一步自定义,只需阅读Google的SpannableString文档,了解有关您可以做什么的更多信息。谢谢您提出这个问题,我想我可能会为您提供一个解决方案 查看中的代码 它是一个用于SpannableString的factory helper类,允许您通过以下方式轻松创建SpannableString:
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);
当然,您可以自定义该类中已有的方法,或者添加您自己的方法,如果您需要进一步自定义,只需阅读Google的SpannableString文档,了解有关您可以做什么的更多信息