TextView实现超链接和电话拨打
MainActivity.java
package com.example.android_textview_html;
import android.os.Bundle;
import android.app.Activity;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
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://baidu.com'>百度</a></big>";
html+="我的电话: + 86 010-8937483+<br/>";
html+="我的电话: + 15995716443+<br/>";
CharSequence charsequence = Html.fromHtml(html);
textview1.setText(charsequence);
textview1.setMovementMethod(LinkMovementMethod.getInstance());//点击的时候产生超链接
String text = "我的URL: http://sina.com\n";
text+= "我的email: abcd@126.com\n";
text+= "我的电话: + 86 010-8937483";
textview2.setText(text);
textview2.setMovementMethod(LinkMovementMethod.getInstance());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<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/textview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20sp"
android:autoLink="all"
android:text="@string/hello_world" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20sp"
android:autoLink="all"
android:layout_marginTop="150sp"
android:text="@string/hello_world" />
</RelativeLayout>
转载于:https://blog.51cto.com/6846041/1415740