A simple way to use WebView in android learning notes

Keywords: Android xml Programming encoding

Android learning notes

Refer to the second edition of the first line of code and the second edition of android programming authority guide

In the development process, we want our App to have the function of visiting web pages, but we don't want to use a third-party browser.

So here we introduce the WebView technology, which is that we can browse the web page on the Activity page.

Let's introduce the simple usage of WebView here.

First, we create the WebView control code in the xml file as follows

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/web_01">
</WebView>

</android.support.constraint.ConstraintLayout>

Second, let's write logic for it in java code

The code is as follows

package com.example.jackson.web_view_01;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView=(WebView) findViewById(R.id.web_01);
        webView.getSettings().setJavaScriptEnabled(true);
        //Let WebView support JavaScript
        webView.setWebViewClient(new WebViewClient());
        //When the web page needs to jump, let it still display in the current WebView interface
        webView.loadUrl("https://baike.baidu.com/item/csdn/172150?fr=aladdin");
        //Incoming jump URL
    }
}

Finally, let's look at the display effect:

Later, we will continue to explore the network technology.

Posted by brob on Fri, 03 Jan 2020 01:16:54 -0800