Getting started with Android (XIX) WebView

Original link: http://www.orlion.ga/676/

WebView can embed a browser in its own application to display web pages.

Create a project webviewdemo and modify the activity_ main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent" >
	
    <WebView 
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

Modify mainactivity.java:

package ga.orlion.webviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

	private WebView webView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		webView = (WebView) findViewById(R.id.web_view);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		webView.loadUrl("http://www.orlion.ga");
	}
}

The code in MainActivity is also very short. First, we use the findViewById () method to get an instance of WebView. Then we call WebView's getSettings () method to set some browser attributes. Here we do not set too many attributes, but we call setJavaScriptEnabled () method to let WebView support JavaScript script. Next is a very important part. We call the setwebviewclient () method of WebView and pass it to

The anonymous class of webviewclient is entered as a parameter, and then the shouldoverrideurlloading () method is overridden. This means that when we need to jump from one page to another, we want the target page to still be displayed in the current WebView instead of opening the system browser. The last step is very simple. Call the loadurl () method of WebView and pass in the web address to display the content of the corresponding web page.

Finally, declare permissions in androidmainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="ga.orlion.webviewdemo"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />
    
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>