TextView中使用链接打开Activity

Activity加入intent-filter支持

<activity android:name="TestActivity">
    <intent-filter>
        <data android:host="yourhost" android:scheme="testschema" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

TextView设置文本

String text = "Click to open activity <a href=\"testschema://yourhost\">open</a>";
textView.setText(Html.fromHtml(text));
textView.setMovementMethod(LinkMovementMethod.getInstance());

注意最后一句话**textView.setMovementMethod(LinkMovementMethod.getInstance());**特别重要,不加的话点击是没有任何效果的

如果希望跳转带上参数,也是可以的,href里面使用

testschema://yourhost?param1=value1&param2=value2

Activity中解析参数使用

String value1 = getIntent().getData().getQueryParameter("param1");
String value2 = getIntent().getData().getQueryParameter("param2");

评论

暂无评论