我需要突出显示textview中的weburl。
为此,我在自己的textview xml中添加了android:autoLink="web"
属性。
如果该网址与youtube视频相关,则需要在我的应用程序的youtube播放器活动中播放该网址,对于其他类型的网址,我希望其在网络浏览器中打开。 因此,如何检测单击哪个URL并查找是否是youtube链接并根据它执行重定向。
以下是我的文本视图中包含的示例文本
这是示例文本
,您可以在此链接上找到漂亮的文章
www.example.com
,这里有一段很好的视频,说明了
www.youtube.com/xyzpqr
进一步阅读请从下载pdf 在这里
www.example.com/pdf/xyz
存在多个链接,因此需要检测单击的链接并对所选链接执行操作。
答案 0 :(得分:0)
您可以在TextView上应用侦听器,并在检测到单击时使用Android中的StartsWith方法检测url是否以您自己开头((youtube的url))(如果匹配),您的youtube播放器中的东西是否会执行重定向
答案 1 :(得分:0)
请在xml
文件的textview中尝试以下属性:
android:autoLink="all"
android:linksClickable="false"
android:textColorLink="#3393FF"
您可以使用包含功能检查URL。
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String URL = textView.getText().toString();
if (URL.contains("youtube") || URL.contains("youtu.be")) {
// then redirect to youtube
} else if (URL.contains("pdf")) {
// then redirect to pdf app
} else {
// then redirect to web
}
}
});
OR
您可以使用下面的库和链接:https://github.com/saket/Better-Link-Movement-Method
implementation 'me.saket:better-link-movement-method:2.2.0'
尝试以下代码:
BetterLinkMovementMethod
.linkify(Linkify.ALL, textView)
.setOnLinkClickListener((textView, url) -> {
// Handle clicks.
if (url.contains("youtube") || url.contains("youtu.be")) {
// then redirect to youtube
} else if (url.contains("pdf")) {
// then redirect to pdf app
} else {
// then redirect to web
}
return true;
})
.setOnLinkLongClickListener((textView, url) -> {
// Handle long-clicks.
return true;
});
答案 2 :(得分:0)
您为什么不使用setMouvementMethod()
呢?
在您的TextView中尝试一下:
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"/>
然后在您的Java类中添加以下行:
TextView myLink = (TextView) findViewById(R.id.myTextView);
myLink.setMovementMethod(LinkMovementMethod.getInstance());