Android WebView与Js交互

WebView,JS交互
WebView
JS交互
WebView
回退:先检查是否可以回退
webView.canGoBack();//是否可以跳到上一页(如果返回false,说明已经是第一页)
webView.goBack();//跳到上个页面 一般用在onBackPressed()函数中。
前进,检查是否可以前进:
webView.canGoForward();//是否可以跳到下一页(如果返回false,说明已经是最后一页)
webView.goForward();//跳到下个页面
停止:
webView.stopLoading();
刷新:
webView.reload();

Activity

// An highlighted block


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

ProgressBar progressBar;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView = findViewById(R.id.map1);
    progressBar = findViewById(R.id.pro);
    findViewById(R.id.b1).setOnClickListener(this);
    findViewById(R.id.b2).setOnClickListener(this);
    findViewById(R.id.b3).setOnClickListener(this);
    findViewById(R.id.b4).setOnClickListener(this);
    initWebView();
}

private void initWebView() {

    webView.loadUrl("https://docs.open.alipay.com/54/104509/");
    webView.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            progressBar.setVisibility(View.GONE);
        }
    });

    webView.setWebChromeClient(new WebChromeClient(){
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            super.onProgressChanged(view, newProgress);
            progressBar.setProgress(newProgress);
        }
    });
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.b1:
            webView.goForward();
            break;
        case R.id.b2:
            webView.goBack();
            break;
        case R.id.b3:
            webView.reload();
            break;
        case R.id.b4:
            webView.stopLoading();
            break;
    }
}
}

xml布局

// An highlighted block


<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=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:text="前进"
        android:id="@+id/b1"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:text="后退"
        android:id="@+id/b2"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:text="刷新"
        android:id="@+id/b3"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:text="暂停"
        android:id="@+id/b4"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
<ProgressBar
    android:id="@+id/pro"
    android:maxHeight="20dp"
    style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
<WebView
    android:id="@+id/map1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></WebView>

</LinearLayout>

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值