1、程序结构图
2、MainActivity.java中的代码
package com.example.setlinkdemo; import java.io.IOException; import org.xmlpull.v1.XmlPullParserException; import android.app.Activity; import android.content.Intent; import android.content.res.ColorStateList; import android.content.res.XmlResourceParser; import android.os.Bundle; import android.text.Html; import android.text.SpannableString; import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.text.style.TextAppearanceSpan; import android.view.View; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_content = (TextView) this.findViewById(R.id.tv_content); setLink(); } public void setLink() { // TODO Auto-generated method stub // 创建一个 SpannableString对象 SpannableString sp = new SpannableString( Html.fromHtml(getString(R.string.set_account_check))); sp.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { // TODO Auto-generated method stub startActivity(new Intent(MainActivity.this, TestActivity.class)); } }, 11, 15, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); ColorStateList csl = null; XmlResourceParser xpplinkcolor = getResources().getXml( R.color.link_color); try { csl = ColorStateList.createFromXml(getResources(), xpplinkcolor); } catch (XmlPullParserException e1) { e1.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } sp.setSpan( new TextAppearanceSpan("monospace", android.graphics.Typeface.NORMAL, (int) tv_content .getTextSize(), csl, csl), 11, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv_content.setText(sp); // 不添加这一句,超链接不能点击 tv_content.setMovementMethod(LinkMovementMethod.getInstance()); } }
3、TestActivity.java中的代码
package com.example.setlinkdemo; import android.app.Activity; import android.os.Bundle; public class TestActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
4、link_color.xml文件中的代码
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#0099cc"/> <!-- pressed --> <item android:state_focused="true" android:color="#0099cc"/> <!-- focused --> <item android:color="#0099cc"/> <!-- default --> </selector>
6、strings.xml文件中的代码
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">SetLinkDemo</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="set_account_check">继续进行意味着您已同意服务协议</string> </resources>
7、AndroidManifest.xml文件中的代码
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.setlinkdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TestActivity" > </activity> </application> </manifest>