Android编程开发之TextView单击链接弹出Activity的方法

时间:2022-09-20 13:19:20

本文实例讲述了android编程开发之textview单击链接弹出activity的方法。分享给大家供大家参考,具体如下:

话不多说直接上码:

核心源码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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());
  }
}

运行效果截图如下:

Android编程开发之TextView单击链接弹出Activity的方法

希望本文所述对大家android程序设计有所帮助。