gpt4 book ai didi

android - Android:在WebView中更改YouTube链接名称

转载 作者:行者123 更新时间:2023-12-03 06:30:31 25 4
gpt4 key购买 nike

我从RSS Feed中获取了一个字符串,有时其中包含指向YouTube视频的链接。我已经能够从字符串中解析URL。

我希望在WebView内将URL替换为“视频链接”,但单击此链接后,应使用YouTube链接。

目前,我替换了字符串,但是单击该字符串时,将转发新字符串,而不转发YouTube URL。

我的代码:

String description = fFeed.getItem(fPos).getDescription();

// get all links from the description string
ArrayList<String> links_in_string = retrieveLinks(description);
Log.d("debug", "All Links: " + links_in_string.toString());

// search for YouTube links
ArrayList<String> resList = new ArrayList<String>();
String searchString = "www.youtube.com/watch?v=";

for (String curVal : links_in_string) {
if (curVal.contains(searchString)) {
resList.add(curVal);
}
}
Log.d("debug", "YouTube Links: " + resList.toString());

// convert to single YouTube URL strings and replace
// them in the description string
Object[] mStringArray = resList.toArray();
for (int i = 0; i < mStringArray.length; i++) {
Log.d("string is", (String) mStringArray[i]);
description = description.replace((String) mStringArray[i],
"Link zum Video");
}


/**
* Retrieve all the links from the description string
* of the RSS Feed
*/
private ArrayList<String> retrieveLinks(String text) {
ArrayList<String> links = new ArrayList<String>();

String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
while (m.find()) {
String urlStr = m.group();
if (urlStr.startsWith("(") && urlStr.endsWith(")")) {
urlStr = urlStr.substring(1, urlStr.length() - 1);
}
links.add(urlStr);
}
return links;
}

更新

我必须第二次搜索网址出现在字符串中。我以这种方式尝试过,但是现在它不能代替URL
// convert to single YouTube URL strings and replace
// them in the description string
Object[] mStringArray = resList.toArray();
for (int i = 0; i < mStringArray.length; i++) {

//replace every second URL by "Link zum Video"
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile((String) mStringArray[i]);
Matcher m = p.matcher(description);
int count = 0;
while (m.find()) {
if (count++ % 2 != 0) {
m.appendReplacement(sb, "Link zum Video");
}
}
m.appendTail(sb);
description = sb.toString();

Log.d("debug", "Description with replaced link: " + description);
}

最佳答案

我没有对其进行测试,但是也许可以使用 anchor 标记:

description = description.replace((String) mStringArray[i],
"<a href=\""+(String) mStringArray[i]+"\">Link zum Video</a>");

关于android - Android:在WebView中更改YouTube链接名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21087223/

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