webView显示网页,
Webview加载的方式:
MainActivity*********************
···
package com.example.webviewtest;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button bt1,bt2,bt3,bt4,bt5;
WebView webView;
LinearLayout lin;
ProgressBar pb;
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
bt1= (Button) findViewById(R.id.bt_1);
bt1.setOnClickListener(this);
bt2= (Button) findViewById(R.id.button2);
bt2.setOnClickListener(this);
bt3= (Button) findViewById(R.id.button3);
bt3.setOnClickListener(this);
bt4= (Button) findViewById(R.id.button4);
bt4.setOnClickListener(this);
bt5= (Button) findViewById(R.id.button5);
bt5.setOnClickListener(this);
lin= (LinearLayout) findViewById(R.id.lin);
img= (ImageView) findViewById(R.id.imageView);
pb= (ProgressBar) findViewById(R.id.progressBar);
webView= (WebView) findViewById(R.id.webView);
webView.loadUrl("http://www.baidu.com");
webView.setWebViewClient(new WebViewClient(){
@Override//处理连接 不写的话所有连接都会默认调用其他软件打开
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
webView.loadUrl(request.getUrl().toString());
return true;
}
@Override//监听网页开始加载
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//显示布局
lin.setVisibility(View.VISIBLE);
pb.setMax(100);
pb.setProgress(0);
}
@Override//监听网页加载结束
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
lin.setVisibility(View.GONE);
}
});
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
pb.setProgress(newProgress);
}
@Override//获取网址标题
public void onReceivedTitle(WebView view, String title) {
setTitle(title);
}
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
img.setImageBitmap(icon);
}
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bt_1://网址
final EditText editText = new EditText(MainActivity.this);
new AlertDialog.Builder(MainActivity.this)
.setTitle("请输入网址")
.setIcon(R.mipmap.ic_launcher_round)
.setView(editText)
.setPositiveButton("确定", new Dialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String url = editText.getText().toString();
if (url!=null&& !url.equals("")){
if (url.indexOf("http://")==0){
webView.loadUrl(url);
}else{
webView.loadUrl("http://"+url);
}
}
}
}).setNegativeButton("取消",null).show();
break;
case R.id.button2://刷新
webView.reload();
break;
case R.id.button3://停止
webView.stopLoading();
break;
case R.id.button4://前进
if(webView.canGoForward()){
webView.goForward();
}else{
Toast.makeText(this, "已经是最前面的页面了", Toast.LENGTH_SHORT).show();
}
break;
case R.id.button5://后退
if(webView.canGoBack()){
webView.goBack();
}else{
Toast.makeText(this, "已经是最后一个页面了", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
···
xml:
···
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.webviewtest.MainActivity">
<LinearLayout
android:id="@+id/lin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:visibility="gone">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher_round" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bt_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="网址" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="刷新" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止" />
<Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="前进" />
<Button
android:id="@+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="后退" />
</LinearLayout>
</LinearLayout>
···