单击 Webview 内的文本时启动 Activity Intent [英] Start Activity Intent on Clicking Text Inside Webview

查看:29
本文介绍了单击 Webview 内的文本时启动 Activity Intent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a webview in my xml which goes like below:

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

我正在像这样加载 webview:

I am loading the webview like this:

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 标记并覆盖函数来触发意图在我的字符串 (webView_text) 中创建链接.在这种情况下它不是这样做的.这里有什么问题?我不确定 Android Webview 是否支持该标签(我认为应该).我在这里有什么错误.提前致谢.

Please notice that I am creating the link in my string (webView_text) by using the html tag and overriding the function to fire an intent. It is not doing in this case. What is the problem here? I am not sure if Android Webview supports the tag (I believe it should). What is my mistake here.Thanks in advance.

推荐答案

您可以通过在清单中的活动意图过滤器中定义方案来实现这一点.对于示例创建活动 (A) 和活动 (B) 并在清单中定义如下:

you can this by define scheme in activity intent filter in manifest. for sample create activity (A) and activity (B) and define in manifest like this :

<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 中有这样的链接:

if in your html have linke like this:

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

当您点击它时,开始活动 B.活动A与此类似.

when you click it , start activity B. Activity A is similar to it.

您可以从源代码

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

NOTE : if using webview for this method you must override the method shouldOverrideUrlLoading() and compare the every url.

这篇关于单击 Webview 内的文本时启动 Activity Intent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆