Android WebView示例

Here you will get android webview example.

在这里,您将获得android webview示例。

Android WebView is a UI component used to open web url or show html data inside the activity.

Android WebView是一个UI组件,用于打开Web URL或在活动中显示html数据。

Below I have given two examples, one is for opening a web url and another is for showing some html data in an activity.

下面我给出了两个示例,一个示例用于打开Web URL,另一个示例用于显示活动中的一些html数据。

Also Read: Android Load Image from URL (Internet) Example

另请参阅: Android从URL加载图像(Internet)示例

Android WebView示例 (Android WebView Example)

开启网址 (Open Web URL)

For opening a url we use loadURL() method of WebView class. By default the url is opened in android browser. As we have to open it inside the activity so we have to provide our own WebViewClient.

为了打开URL,我们使用WebView类的loadURL()方法。 默认情况下,网址是在android浏览器中打开的。 由于我们必须在活动中打开它,因此我们必须提供自己的WebViewClient。

In this example the activity consists of three UI components namely EditText, Button and WebView. When user will enter url of a website in the edittext and click on button then the website will open in webview.

在此示例中,活动包括三个UI组件,即EditText,Button和WebView。 当用户在编辑文本中输入网站的网址并单击按钮时,该网站将在webview中打开。

Create an android project with package name thecrazyprogrammer.androidexample and add following code in respective files.

创建一个程序包名为thecrazyprogrammer.androidexample的android项目,并在相应文件中添加以下代码。

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
 
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/url"
        android:hint="Enter the url to open..."/>
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Open"
        android:id="@+id/openBtn"/>
 
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"/>
</LinearLayout>

MainActivity.java

MainActivity.java

package thecrazyprogrammer.androidexample;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity {
    WebView webView;
    Button openBtn;
    EditText url;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        webView = (WebView)findViewById(R.id.webView);
        openBtn = (Button)findViewById(R.id.openBtn);
        url = (EditText)findViewById(R.id.url);
 
        openBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.setWebViewClient(new CustomWebClient());
                webView.loadUrl(url.getText().toString());
            }
        });
    }
 
    public class CustomWebClient extends WebViewClient
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

Note: As we are fetching data from internet so we have to provide internet access permission. Just add following line in AndroidManifest.xml file.

注意:由于我们正在从Internet上获取数据,因此我们必须提供Internet访问权限。 只需在AndroidManifest.xml文件中添加以下行。

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

Output

输出量

Android WebView Example 1

显示HTML数据 (Show HTML Data)

The loadData() method of WebView class is used to show html data. It can be done in following way.

WebView类的loadData()方法用于显示html数据。 可以通过以下方式完成。

Create an android project with package name thecrazyprogrammer.androidexample and add following code in respective files.

创建一个程序包名为thecrazyprogrammer.androidexample的android项目,并在相应文件中添加以下代码。

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical">
 
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"/>
</LinearLayout>

MainActivity.java

MainActivity.java

package thecrazyprogrammer.androidexample;
 
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
 
public class MainActivity extends Activity {
    WebView webView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        webView = (WebView)findViewById(R.id.webView);
        webView.loadData("<h1>Android WebView Example</h1>","text/html","UTF-8");
    }
}

Output

输出量

Android WebView Example 2

Comment below if you found anything incorrect or have doubts related to above android webview example.

如果发现任何不正确的内容或与上述android webview示例相关的疑问,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/07/android-webview-example.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值