Android玄铁剑之TextView之我要穿越
传送门 ☞ Android兵器谱 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229
玄铁剑
玄铁乃天下至宝,便是要得一两也是绝难,寻常刀枪剑戟之中,只要加入半两数钱,凡铁立成利器。“重剑无锋,大巧不工”,其中境界,远胜世上诸般最巧妙的剑招。
本节我们学习如何利用Android平台“玄铁剑”TextView导航到一个Activity,下面给出该情景的案例:
一、案例技术要点
1.android.text.SpannableString:提供一个总体文本是不可变的,但是支持局部对象(它所标记的)可以附加或分离。
2.SpannableString类setSpan(...):设置链接地址、链接显示内容和附加内容应用效果。
3.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE:表示在当前文本前端和后端新增字符均不采用当前显示效果。
4.android.text.method.LinkMovementMethod:提供超链接功能。TextView需要此功能时引入该类的实例即可。
二、案例代码陈列
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.textview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".TextViewMainActivity" 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=".TextViewOneActivity" /> <activity android:name=".TextViewTwoActivity" /> </application> </manifest>strings.xml
<resources> <string name="app_name">Textview单击链接弹出Activity</string> </resources>main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/tv1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" /> <TextView android:id="@+id/tv2" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:layout_marginTop="20dp" /> </LinearLayout>TextViewMainActivity.java
package com.android.textview; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.SpannableString; import android.text.Spanned; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.view.View; import android.widget.TextView; /** * TextView案例三:单击链接弹出Activity * @author lynnli1229 */ public class TextViewMainActivity extends Activity { private TextView textView1, textView2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView1 = (TextView) findViewById(R.id.tv1); textView2 = (TextView) findViewById(R.id.tv2); String textStr1 = "点击链接到TextViewOneActivity"; String textStr2 = "点击链接到TextViewTwoActivity"; //拆分字符串 SpannableString ss1 = new SpannableString(textStr1); SpannableString ss2 = new SpannableString(textStr2); ss1.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Intent intent = new Intent(TextViewMainActivity.this, TextViewOneActivity.class); startActivity(intent); } }, 0, textStr1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss2.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Intent intent = new Intent(TextViewMainActivity.this, TextViewTwoActivity.class); startActivity(intent); } }, 0, textStr2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView1.setText(ss1); textView2.setText(ss2); textView1.setMovementMethod(LinkMovementMethod.getInstance()); textView2.setMovementMethod(LinkMovementMethod.getInstance()); } }TextViewOneActivity.java
package com.android.textview; import android.app.Activity; import android.os.Bundle; public class TextViewOneActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("This is TextViewOneActivity"); } }TextViewTwoActivity.java
package com.android.textview; import android.app.Activity; import android.os.Bundle; public class TextViewTwoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("This is TextViewTwoActivity"); } }三、案例效果展示