WebView的基本使用

    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:onClick="load"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="加载网页"/>
    </LinearLayout>

    <ProgressBar
        android:id="@+id/webView_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
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:onClick="refresh"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="刷新"
            />
        <Button
            android:onClick="advance"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="前进"
            />
        <Button
            android:onClick="back"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="后退"
            />
    </LinearLayout>

</LinearLayout>
package bwie.com.mywebview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText path_et;
    private ProgressBar webView_pb;
    private WebView mWeb;
    private WebSettings mSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        path_et =  findViewById(R.id.path_et);
        webView_pb =  findViewById(R.id.webView_pb);
        mWeb=  findViewById(R.id.www_wb);
        //点击加载网页,不跳到浏览器
        mWeb.setWebViewClient(new WebViewClient());
        //requestFocus 触摸焦点起作用(如果不设置,则在点击网页文本输入框时,不能弹出软键盘及不响应其他的一些事件
        mWeb.requestFocus();
        mWeb.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                //显示出来
                webView_pb.setVisibility(View.VISIBLE);
                //进行更新
                webView_pb.setProgress(newProgress);
                //如果进度条为一百则隐藏
                if (newProgress==100){
                    //隐藏
                    webView_pb.setVisibility(View.GONE);
                }
            }
        });
        initWeb();
    }

    private void initWeb() {
        mSettings= mWeb.getSettings();
        //支持js
        mSettings.setJavaScriptEnabled(true);
        //允许网页缩放
        mSettings.setSupportZoom(true);
    }

    //加载
    public void load(View view){
        String et = path_et.getText().toString().trim();
        if (TextUtils.isEmpty(et)) {
            Toast.makeText(this, "请用户输入网址,直接从www开始即可", Toast.LENGTH_SHORT).show();
            return;
        }
        mWeb.loadUrl("http://"+et);
    }
    //刷新
    public void refresh(View view){
        //对网页进行刷新
        mWeb.reload();
    }
    //前进
    public void advance(View view){
        //对网页进行能否前进的判断
        if (mWeb.canGoForward()){
            //前进
            mWeb.goForward();
        }else {
            Toast.makeText(MainActivity.this,"这是最新的页面",Toast.LENGTH_SHORT).show();
        }
    }
    //后退
    public void back(View view){
        //对网页进行能否后退的判断
        if (mWeb.canGoBack()){
            //后退
            mWeb.goBack();
        }else {
            Toast.makeText(MainActivity.this,"这是最后一个的页面",Toast.LENGTH_SHORT).show();

        }
    }
}

上面是一个一个webView的基本使用
后面是WebView和js的交互

<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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/path_et"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:hint="输入访问网址,直接从www开始即可"/>
        <Button
            android:onClick="load"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="加载"/>
    </LinearLayout>

    <WebView
        android:id="@+id/show_wb"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="match_parent">
    </WebView>

    <Button
        android:onClick="call"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="调用js的方法"/>
</LinearLayout>
package bwie.com.myjs;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    private static final String HTML_URL = "http://169.254.53.96:8080/test.html";
    private WebView show_wb;
    private WebSettings mSettings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    @SuppressLint("JavascriptInterface")
    private void initView() {
        show_wb =  findViewById(R.id.show_wb);
        //支持js
        mSettings = show_wb.getSettings();
        mSettings.setJavaScriptEnabled(true);
        //支持缩放
        mSettings.setSupportZoom(true);
        //打开网页不调用浏览器而是在本页面执行
        show_wb.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });
        //调用js的方法回调参数进行吐司
        show_wb.addJavascriptInterface(new Object(){
            @JavascriptInterface
            public void showToast(String context){
                Toast.makeText(MainActivity.this,context,Toast.LENGTH_SHORT).show();
            }
        },"Android");
    }

    public void call(View view){
        //调用js方法给js赋值
        show_wb.loadUrl("javascript:changeInputValue('我是谁,我在那,我在干什么。')");
    }
    public void load(View v){
        //点击加载网页
        show_wb.loadUrl(HTML_URL);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值