我正在制作一个基于json网络服务的应用程序。我遇到了一些问题,我有一个字符串,我必须拆分它然后点击。让我清楚我的问题:
- >字符串我有类似的东西:“#outfit#fashion #color”或“#outfit”或“ #outfit 真的太棒了。“
- >现在我想将子串的颜色更改为蓝色: #outfit , #fashion , #color 等。 ..和rest substring会变黑。
- >此外我想让它可点击例如: #outfit 会点击并获取与 #outfit 标记相关的所有数据。与标签概念相同。
- >我有很多类型的字符串,我希望逻辑在每种情况下都能正常工作:
在此先感谢。只想让所有标签都是蓝色并点击。请用完整的代码描述我,因为我的逻辑不太好..
答案 0 :(得分:2)
您可以使用 SpannableString
SpannableString ss = new SpannableString("Hi this is #Naveen. I'll meet #Peter in the evening.. Would you like to join #Sam??");
ClickableSpan clickableSpanNaveen = new ClickableSpan() {
@Override
public void onClick(View textView) {
//Do Stuff for naveen
}
};
ClickableSpan clickableSpanPeter = new ClickableSpan() {
@Override
public void onClick(View textView) {
//Do Stuff for peter
}
};
ClickableSpan clickableSpanSam = new ClickableSpan() {
@Override
public void onClick(View textView) {
//Do Stuff for sam
}
};
ss.setSpan(clickableSpanNaveen, 11, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(clickableSpanPeter, 29, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(clickableSpanSam, 76, 79, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView contentTextView=(TextView)userHeader.findViewById(R.id.contentTextView);
contentTextView.setText(ss);
contentTextView.setMovementMethod(LinkMovementMethod.getInstance());
我从here
复制此代码但是你必须找到一种方法来检测每个跨度的开始和结束。
indexOf("#")
,并将这些索引存储在数组中。indexOf(" ", // index of "#" //)
找到他后面的第一个空格“”。 (注意:只有在所有可点击字符串中没有空格时,此逻辑才有效)答案 1 :(得分:0)
您认为可以使用ClickableSpan ..让我通过示例解释一下.. myString ="点击此处阅读更多"
我想和#34;在这里"单词可点击(蓝色)..
SpannableString ReadMore = new SpannableString(myString);
ClickableSpan clickReadMore = new ClickableSpan() {
@Override
public void onClick(View textView) {
//do your stuff here when "here" is clicked
}
};
// below 6 is start index of clickable string and 10 is end index of clickable string
ReadMore.setSpan(clickReadMore, 6, 10,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
myTextView.setText(ReadMore);
myTextView.setMovementMethod(LinkMovementMethod
.getInstance());
使用上面的示例,您可以拆分字符串并使用单独的ClickableSpan
使其可单击答案 2 :(得分:0)
这是一种可以处理输入以准备从标记创建超链接的方法。
import java.util.ArrayList;
import java.util.regex.*;
public class Hashtag {
public static void main(String[] args) {
String input = "#outfit#fashion#color\n"
+ "#outfit\n"
+ "#outfit is really awsome.\n"
+ "check my new #dress, the brand #new #outfit. Isn't it #cool?\n";
Pattern pattern = Pattern.compile("#([a-zA-Z]+)");
Matcher matcher = pattern.matcher(input);
// will store the parsed input string
ArrayList<Text> parsed = new ArrayList<Text>();
// populate the ArrayList
int previousPosition = 0;
while (matcher.find()) {
String previousText = input.substring(previousPosition, matcher.start());
if (!previousText.isEmpty()) {
parsed.add(new Text(previousText));
}
parsed.add(new Tag(matcher.group(1)));
previousPosition = matcher.end();
}
String trailingText = input.substring(previousPosition, input.length());
if (!trailingText.isEmpty()) {
parsed.add(new Text(trailingText));
}
// the input string
for (Text t : parsed) {
if (t instanceof Tag) {
Tag tag = (Tag)t;
/* Here you would do some special handling of the tags to make them clickable
* tag.getTag(); can be used to get the contents of tag. For example, the
* for a "#OutFit" tag the getTag() method returns "outfit"
*/
System.out.print("[" + tag + "]");
} else {
System.out.print(t);
}
}
}
public static class Text {
private String text;
Text(String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}
public static class Tag extends Text
{
private String tag;
Tag(String tag) {
super("#" + tag);
this.tag = tag.toLowerCase();
}
public String getTag() {
return tag;
}
}
}
以上代码产生以下输出。它将标签放在方括号中,以证明提取有效。
[#outfit][#fashion][#color]
[#outfit]
[#outfit] is really awsome.
check my new [#dress], the brand [#new] [#outfit]. Isn't it [#cool]?
从这一点开始,应该很容易显示文本并使标签可以点击。
答案 3 :(得分:0)
经过长时间的努力,我找到了精彩的教程:
how-to-implment-hashtags-and-callouts-in-android
@Rami的答案适用于静态字符串,但本教程适用于动态数据。 希望它对某人有所帮助。