通过如下这种方式给 TextView 添加的链接支持链接样式、点击事件、href 样式,代码如下:
package cn.sunzn.tlink; import android.app.Activity; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv_sunzn = (TextView) findViewById(R.id.tv_sunzn); tv_sunzn.setMovementMethod(LinkMovementMethod.getInstance()); Spanned text = Html.fromHtml("博客园主页:<a href="http://www.cnblogs.com/sunzn/">sunzn'Blog</a>"); tv_sunzn.setText(text); } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/tv_sunzn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sunzn" android:textSize="20sp" /> </RelativeLayout>