Nineth Chapter, WebView click on the link in the page to jump to other activity (Android)

Source: Internet
Author: User

Add Network permissions in Androidmanifest.xml

<uses-permission android:name= "Android.permission.INTERNET"/>
Activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <button        android:id=" @+id/main_button "        android:layout_width=" Match_parent "        android:layout_height= "wrap_content"        android:text= "Start"/>    <webview        android:id= "@+id /main_web "        android:layout_width=" match_parent "        android:layout_height=" Match_parent "/></ Linearlayout>
Mainactivity.java

Package Com.example.webviewdemo01;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.view.View.OnClickListener; Import Android.webkit.webchromeclient;import Android.webkit.webview;import Android.webkit.webviewclient;import Android.widget.button;public class Mainactivity extends Activity implements Onclicklistener {private Button Main_button ;p rivate WebView web_view; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main);//Initialize Data Initview ();} private void Initview () {//TODO auto-generated method Stubmain_button = (Button) This.findviewbyid (R.id.main_button); Web_view = (WebView) This.findviewbyid (r.id.main_web);//Set Listener Main_button.setonclicklistener (this);} @Overridepublic void OnClick (View arg0) {//TODO auto-generated method Stubweb_view.loadurl ("http://www.baidu.com");// Webchromeclienty dialog box for handling WebView JavaScript,//Website icon, site title, loading progress, etc., rewrite the method can be web_view.setwebchromeclient (new webchromeclient ());//This method is not written here// Webviewclient used to handle webview various notifications, request events, etc., rewrite the method can be web_view.setwebviewclient (new Webviewclient () {// Clicking on a link in the page will call this method @overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {//TODO auto-generated Method stub//jumps to another activityintent intent = new Intent (Getapplication (), secondactivity.class); startactivity (intent); LOG.I ("Qing", "shouldoverrideurlloading ..." + URL); return super.shouldoverrideurlloading (view, URL);}});}}
Run:



Click on any one of the links above


Note: The code and layout of the second activity is not posted because there is not much content written.

Nineth Chapter, WebView click on the link in the page to jump to other activity (Android)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.