webview使用时只需要在清单文件中添加一个网络权限就可以了
webview主要是用来和js交互的
//在本页面加载网页,不调用系统浏览器
webView.setWebViewClient(new WebViewClient());
//支持js
WebSettings settings = webView.getSettings();
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setJavaScriptEnabled(true);
以下是代码
package com.example.lx_webview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.Toast; import java.util.concurrent.TimeUnit; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private WebView www_wb; private ProgressBar webView_pb; private EditText path_et; private View btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 initView(); //使用webview对象,进行初始化 webViewinit(); //得到webviewSettings对webview进行初始化设置 webviewSettingSInit(); } //点击事件 @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn: String trim = path_et.getText().toString().trim(); if (TextUtils.isEmpty(trim)){ Toast.makeText(this,"不能输入为空",Toast.LENGTH_SHORT).show(); return; } www_wb.loadUrl("http://"+trim); break; } } //得到webviewSettings对webview进行初始化设置 private void webviewSettingSInit() { WebSettings settings = www_wb.getSettings(); //允许js交互 settings.setJavaScriptEnabled(true); //设置可以支持缩放 settings.setSupportZoom(true); } //使用webview对象,进行初始化 private void webViewinit() { //点击在App内执行 www_wb.setWebViewClient(new WebViewClient()); //WebView加载网页监听,其进度和我们的进度条相绑定,然后显示出我们也想的效果 www_wb.setWebChromeClient(new WebChromeClient(){ //方法中的onProgressChanged就代表了其速度 @Override public void onProgressChanged(WebView view, int newProgress) { //首先我们的进度条是隐藏的 webView_pb.setVisibility(View.VISIBLE); //把网页加载的进度传给我们的进度条 webView_pb.setProgress(newProgress); if (newProgress == 100){ //加载完毕让进度条消失 webView_pb.setVisibility(View.GONE); } super.onProgressChanged(view, newProgress); } }); } //初始化 private void initView() { path_et = findViewById(R.id.path_et); webView_pb = findViewById(R.id.pb); www_wb = findViewById(R.id.www_wb); btn = findViewById(R.id.btn); btn.setOnClickListener(this); } }
布局
<?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"> <!-- 用到了权重知识,progress进度条模式:style="?android:attr/progressBarStyleHorizontal" --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/path_et" android:layout_width="match_parent" android:layout_weight="1" android:layout_height="wrap_content" android:singleLine="true" android:hint="请用户输入网址,直接从www开始即可" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="加载网页" /> </LinearLayout> <ProgressBar android:id="@+id/pb" android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:visibility="gone" /> <WebView android:id="@+id/www_wb" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"> </WebView> </LinearLayout>