Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

android - Start Activity Intent on Clicking Text Inside 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"/>

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;
            }
        });

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

if in your html have linke like this:

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

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

you can get source code from Source Code

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...