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>
在这里插入图片描述