android - 在 Webview 中单击文本启动 Activity Intent

标签 android android-intent android-activity android-webview android-xml

我的 xml 中有一个 webview,如下所示:

 <WebView
        android:id="@+id/webView"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

我正在这样加载 webview:

String webView_text = "Lorem ipsum..............**<a><u>Link to fire intent</u></a>**";

 WebView webView= (WebView) findViewById(R.id.webView);
webView.loadData(String.format(htmlText, webView_text), "text/html", "utf-8");
        webView.setWebViewClient(new WebViewClient()
        {
            // Override URL
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                Intent intent = new Intent(getApplicationContext(),OtherActivity.class);
                startActivity(intent);
                return true;
            }
        });

请注意,我正在通过使用 html 标记并覆盖函数来触发 Intent 来在我的字符串 (webView_text) 中创建链接。在这种情况下它没有这样做。这里有什么问题?我不确定 Android Webview 是否支持该标签(我相信它应该支持)。我在这里犯了什么错误。提前致谢。

最佳答案

您可以通过在 list 中的 Activity Intent 过滤器中定义方案来做到这一点。 对于示例创建 Activity (A) 和 Activity (B) 并在 list 中定义如下:

<activity android:name="A" >
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="activity_a" />
    </intent-filter>
</activity>
<activity android:name="B" >
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT" />
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="activity_b" />
    </intent-filter>
</activity>

如果在您的 html 中有这样的链接:

<a href="activity_b://b">Activity B</a>

当您单击它时,启动 Activity B。 Activity A与之类似。

您可以从Source Code 获取源代码

注意:如果将 webview 用于此方法,您必须覆盖方法 shouldOverrideUrlLoading() 并比较每个 url。

关于android - 在 Webview 中单击文本启动 Activity Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20791482/

相关文章:

android - IllegalArgumentException - WindowManagerGlobal.java

android - Chrome 检查器中未显示 WebView

java - 为什么要在发送 Intent 后调用 finish() 方法?

android.util.AndroidRuntimeException : You cannot combine custom titles with other title features

java - 正则表达式从区分大小写变为不区分大小写

android - 从全屏切换到非全屏弹出/幻灯片

android - 通过 Android 中的 Intent 传递复杂对象

android - 从 Intent 调用时如何恢复 Activity

android - 背景时如何使 Activity 模糊?

android - 类型的预期接收者,但得到了 android.support.v7.widget.TintContextWrapper