我通过 android:autoLink="web" 属性在我的文本视图中将文本显示为链接。它显示成功。但是现在我也想将文本显示为从@开始的链接,例如“用户通过@FleeGroups按下这篇文章的FOH按钮”中的“FleeGroups”
问问题
92 次
3 回答
1
使用可扩展的字符串
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s= "User pressed FOH button of this post via @FleeGroups";
tv = (TextView) findViewById(R.id.tv);
String split[] = s.split("@");
SpannableString ss1= new SpannableString(split[1]);
Log.i("....",""+split[0]+"........."+split[1]);
ss1.setSpan(new MyClickableSpan(split[1]), 0,split[1].length(), 0);
tv.append(split[0]);
tv.append(ss1);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
class MyClickableSpan extends ClickableSpan
{
String mystring;
public MyClickableSpan(String s)
{
mystring =s;
}
@Override
public void updateDrawState(TextPaint ds) {
// TODO Auto-generated method stub
super.updateDrawState(ds);
ds.setColor(Color.BLUE);
}
@Override
public void onClick(View widget) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, mystring, 1000).show();
}
}
}
更多关于造型@
http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring
快照
供以后需要时参考。
您还可以使用正则表达式匹配以 @ 开头的单词
String s= "User pressed @FOH button of this post via @FleeGroups some text";
Matcher matcher = Pattern.compile("@\\s*(\\w+)").matcher(s);
while (matcher.find()) {
spanstring= matcher.group(1);
Log.i(".............",spanstring);
}
于 2013-07-10T11:12:37.797 回答
1
/*Method in which you can pass the string to convert the into
spannableString and call this method form where ever you want
to set the text. It even work if you have mutiple @ symbols
in your string.*/
TextView tv=(TextView) findViewById(R.id.textview); tv.setText(getSpannableString("hi @StackOverFlow android"));
public SpannableStringBuilder getSpannableString(String str) {
SpannableStringBuilder builder = new SpannableStringBuilder();
String feed = str.replaceAll("\n", " ");
String[] individualfeed = feed.split(" ");
for (int i = 0; i < individualfeed.length; i++) {
if (individualfeed[i].contains("@")
) {
SpannableString redSpannable = new SpannableString(
individualfeed[i] + " ");
Pattern p = Pattern.compile(".*(\\w+)");
Matcher m = p.matcher(individualfeed[i]);
String str123 = null;
if (m.find()) {
str123 = m.group(1);
}
int startFrom = 0;
if (individualfeed[i].contains("@")) {
startFrom = individualfeed[i].indexOf("@");
}
if(individualfeed[i].trim().length()==1)
{
builder.append(individualfeed[i] + " ");
continue;
}
// I am using Green Color in this code change it accordingly
redSpannable.setSpan(
new ForegroundColorSpan(Color.parseColor("#00FF00")),
startFrom, individualfeed[i].lastIndexOf(str123) + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
final String tag = (String) individualfeed[i].subSequence(
startFrom, individualfeed[i].lastIndexOf(str123) + 1);
builder.append(redSpannable);
} else {
builder.append(individualfeed[i] + " ");
}
}
return builder;
}
于 2013-07-10T11:18:36.977 回答
0
您可以使用Html.fromHtml()
然后设置LinkMovementMethod
移动方法。
像这样:
String link = "<a href=\"http://google.com\">@FleeGroups</a>";
String message = "User pressed FOH button of this post via ";
textView.setText(Html.fromHtml(message + link));
textView.setMovementMethod(LinkMovementMethod.getInstance());
于 2013-07-10T11:08:14.860 回答