我在文本视图中通过android:autoLink =“web”属性显示文本作为链接。它显示成功。但现在我也希望将文本显示为从@开头的链接,例如“用户通过@FleeGroups按下此帖子的用户按下FOH按钮”中的“FleeGroups”
答案 0 :(得分:1)
使用Spanable String
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);
}
答案 1 :(得分: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;
}
答案 2 :(得分: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());