如何在Android Webview中使网站上的打印按钮工作?

时间:2019-05-16 16:20:26

标签: javascript android html5 android-webview

我的网站上有一个关于模式的按钮,在该按钮上附加了一个处理程序函数以打印模式。该处理程序具有处理“仅打印模式”的代码,并最终运行window.print()。该按钮的功能类似于浏览器中的超级按钮。但是在Android Webview中似乎无法使用。这是应用程序的所有设置代码。如果有的话,请提供完整的工作答案,因为我是Android和Java的新手。

//MainActivity.java//

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    private WebView mywebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebView = (WebView)findViewById(R.id.mywebView);
        WebSettings webSettings=mywebView.getSettings();
        mywebView.loadUrl("https://mywesite.com/");
        mywebView.setWebViewClient(new myWebClient());
        webSettings.setJavaScriptEnabled(true);
        mywebView.setHorizontalScrollBarEnabled(false);
    }

    public class myWebClient extends WebViewClient
    {


        @Override
        public void onPageFinished(WebView view, String url) {

            //show webview
            findViewById(R.id.mywebView).setVisibility(View.VISIBLE);
            //hide loading image
            findViewById(R.id.imageView1).setVisibility(View.GONE);
            findViewById(R.id.progressBar1).setVisibility(View.GONE);

        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;

        }

    }




    @Override
    public void onBackPressed () {
        if (mywebView.canGoBack()) {
            mywebView.goBack();
        } else{
            super.onBackPressed();
        }

    }

}





////activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#ffffff"
    android:orientation="vertical"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="81dp">


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:contentDescription="@string/logo"
        android:src="@drawable/aptsplash"
        android:visibility="visible"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />


    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="80dp"
        android:layout_height="65dp"
        android:layout_centerInParent="true"
        android:scrollbarStyle="insideOverlay"
        android:visibility="visible"
        tools:layout_editor_absoluteX="144dp"
        tools:layout_editor_absoluteY="397dp" />

    <WebView
        android:id="@+id/mywebView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone"
        tools:layout_editor_absoluteX="106dp"
        tools:layout_editor_absoluteY="421dp" />


</RelativeLayout>

0 个答案:

没有答案