//布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.hzx.text_view1.MainActivity">
<TextView
android:id="@+id/textview1"
android:padding="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textview2"
android:padding="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="all"
android:textSize="20sp"
/>
</LinearLayout>
//activity
public class MainActivity extends AppCompatActivity {
private TextView textView1, textView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView)this.findViewById(R.id.textview1);
textView2 = (TextView)this.findViewById(R.id.textview2);
//添加一段html的标志
String html = "<font color='red'>I love android</font><br>";
html+="<font color='#0000ff'><big><i>I love android</i></big></font><p>";
html+="<big><a href='http://www.baidu.com'>百度</a></big>";
CharSequence charSequence = Html.fromHtml(html);
textView1.setText(charSequence);
textView1.setMovementMethod(LinkMovementMethod.getInstance());//点击时产生超链接
String text = "我的URL: http:www.hao123.com\n";
text+="我的email:abcd@126.com\n";
text+="我的电话: + 86 010-89487389";
textView2.setText(text);
textView2.setMovementMethod(LinkMovementMethod.getInstance());
}
}
网友评论