1 修改XML文件即可,[color=red]android:autoLink="web"[/color]
[code=xml"]
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="@string/hello" />
[/code]
autoLink有好几种类型:web phone all等。
2 修改链接的默认点击事件
[code=xml"]
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:text="@string/hello" />
[/code]
autoLink有好几种类型:web phone all等。
2 修改链接的默认点击事件
public class HtmlAllTestActivity extends Activity{
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.tv = (TextView)findViewById(R.id.text_view);
CharSequence text = tv.getText();
if (text instanceof Spannable) {
int end = text.length();
Spannable sp = (Spannable) tv.getText();
URLSpan[] urls = sp.getSpans(0, end, URLSpan.class);
SpannableStringBuilder style = new SpannableStringBuilder(text);
style.clearSpans();// should clear old spans
for (URLSpan url : urls) {
MyURLSpan myURLSpan = new MyURLSpan(url.getURL());
style.setSpan(myURLSpan, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
tv.setText(style);
}
}
private class MyURLSpan extends ClickableSpan {
private String mUrl;
MyURLSpan(String url) {
mUrl = url;
}
@Override
public void onClick(View widget) {
Toast.makeText(HtmlAllTestActivity.this, mUrl, Toast.LENGTH_LONG).show();
widget.setBackgroundColor(Color.parseColor("#00000000"));
}
}
}