特殊字符我有一個3串這樣的:如何獲得與單詞和其Click事件
"@Username: Deliverd your order",
"YOU got trophy: KING OF COINS",
"There is a package waiting for you to pick up from #surat to #mumbai",
我想要做的就是用戶名和城市名不同的顏色到其點擊事件。
我能達到的是通過分割爲「:」字符獲取用戶名。 但我不知道如何獲得城市名稱,並點擊兩者的事件。
在城市名稱中只有最後一個城市的顏色正在變化,如何更改兩個城市的名稱顏色並獲取其點擊事件。
這是我的嘗試:
if (notifications.getTitle().contains(":"))
{
String[] username = notifications.getTitle().split(":");
String uname = getColoredSpanned(username[0] + ":", "#ff7505");
String txt = getColoredSpanned(username[1], "#000000");
holder.txtTitle.append(Html.fromHtml(uname +" " + txt));
holder.txtTitle.setMovementMethod(LinkMovementMethod.getInstance());
}
else if (notifications.getTitle().contains("#"))
{
Matcher matcher =
Pattern.compile("#\\s(\\w+)").matcher(notifications.getTitle());
i=0;
while (matcher.find())
{
place.add(i, matcher.group(1));
i++;
}
String place1 = getColoredSpanned("#" + place.get(0), "#237BCD");
String place2 = getColoredSpanned("#" + place.get(1), "#237BCD");
places1 = notifications.getTitle().replace("#" + place.get(0), place1);
places1 = notifications.getTitle().replace("#" + place.get(1), place2);
holder.txtTitle.setText(Html.fromHtml(places1));
}
else
{
holder.txtTitle.setText(notifications.getTitle());
}
private String getColoredSpanned(String text, String color) {
String input = "<font color=" + color + ">" + text + "</font>";
return input;
}
,這就是我得到的輸出:
,這是我真正期待:
https://stackoverflow.com/questions/10696986/how-to-set-the-part-of-the-text-view-is-clickable – vlatkozelka
至於如何從文本中獲取數據,請嘗試格式化你的字符串更容易解析,而不是使用split(),也許是一個json。 – vlatkozelka
我試圖與您的鏈接,但我可以得到點擊事件,但無法設置顏色.. –