Android开发(27)--TextView单击链接弹出Activity
话不多说直接上码:
核心源码:
package com.example.textview4; 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; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv1 = (TextView) findViewById(R.id.textView1); TextView tv2 = (TextView) findViewById(R.id.textView2); String text1 = "显示第一个activity"; String text2 = "显示第二个activity"; // 将text进行拆分 SpannableString ss1 = new SpannableString(text1); SpannableString ss2 = new SpannableString(text2); ss1.setSpan( new ClickableSpan() { @Override public void onClick(View widget) { Intent intent = new Intent(MainActivity.this, FirstActivity.class); startActivity(intent); } }, 0, text1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); ss2.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); } }, 0, text2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tv1.setText(ss1); tv2.setText(ss2); tv1.setMovementMethod(LinkMovementMethod.getInstance()); tv2.setMovementMethod(LinkMovementMethod.getInstance()); } }