WebView的基本使用
下面是xml代码
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.webviewdemo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<EditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_weight="5"
android:hint="@string/edit_text" />
<Button
android:id="@+id/enter"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/enter" />
</LinearLayout>
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
下面是java代码
package com.example.webviewdemo;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private WebView webView;
private EditText editText;
private Button enter;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到我们需要的控件
webView = (WebView) findViewById(R.id.web_view);
editText = (EditText) findViewById(R.id.edit_text);
enter = (Button) findViewById(R.id.enter);
// 给按钮设置点击事件
enter.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// WebView加载用户输入的资源地址
webView.loadUrl(editText.getText().toString());
// 覆盖WebView默认通过第三方或者系统浏览器打开网页的行为,使网页可以在WebView中打开
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// 返回值为true时,控制网页在WebView中打开,为false时则是通过第三方或者系统浏览器打开
view.loadUrl(request.toString());
return true;
}
});
// 启用 JavaScript的支持
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
// WebView优先使用缓存加载
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.setWebChromeClient(new WebChromeClient() {
@Override // 监听网页加载进度的方法
public void onProgressChanged(WebView view, int newProgress) {
// newProgress是一个1-100的整数进度
if (newProgress == 100) {
// 加载完成,关闭进度条(progressDialog)
closeDialog();
} else {
// 正在加载,打开进度条(progressDialog)
openDialog(newProgress);
}
super.onProgressChanged(view, newProgress);
}
});
}
private void openDialog(int newProgress) {
// 判断是否有进度条
if (dialog == null) {
dialog = new ProgressDialog(this);
dialog.setTitle(getString(R.string.dialog_title));
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
} else {
// 刷新dialog的进度
dialog.setProgress(newProgress);
}
}
private void closeDialog() {
// 判断dialog不为空,并且是显示状态
if (dialog != null && dialog.isShowing()) {
// 关闭dialog
dialog.dismiss();
// 把dialog置为空
dialog = null;
}
}
// 改写物理按键---返回的逻辑
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 判断keycode等于系统返回键
if (keyCode == KeyEvent.KEYCODE_BACK) {
// 判断是否有页面可以返回
if (webView.canGoBack()) {
// 返回上一页面
webView.goBack();
return true;
} else {
// 退出程序
System.exit(0);
}
}
return super.onKeyDown(keyCode, event);
}
}
这只是简单的使用WebView的代码