gpt4 book ai didi

android - Window.Print 在 Android WebView (API 23) 中不起作用,但在默认浏览器和 Chrome 中起作用

转载 作者:行者123 更新时间:2023-12-03 04:41:16 26 4
gpt4 key购买 nike

以下示例代码来自 here在 Galaxy Tab S2 上的默认浏览器“Internet”和“Chrome”浏览器中都可以正常工作。

<!DOCTYPE html>
<html>
<body>

<p>Click the button to print the current page.</p>

<button onclick="myFunction()">Print this page</button>

<script>
function myFunction() {
window.print();
}
</script>

</body>
</html>

但是,当托管在 WebView 中时,单击“打印此页”时不会发生任何事情

我的 View XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.company.MainActivity"
tools:showIn="@layout/activity_main">

<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

MainActivity.Java 具有以下内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
WebViewClient mWebClient = new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
};
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(mWebClient);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setGeolocationEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.loadUrl("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_print");
}

WebView 开发人员文档 here明确指出:

您不能在 HTML 文档中使用 JavaScript 来触发打印。

如何托管网页并允许 JavaScript 打印工作。

我想到过但还没有找到实现方法的替代方案:

  • 拦截按钮点击,如果点击打印按钮则触发我自己的打印代码
  • 将“打印”按钮 HTML 即时替换为其他一些代码,以便我可以在应用程序中处理它

到目前为止,为了解决此限制,我已将“打印”按钮添加到我的应用程序中并运行我自己的打印代码。它工作正常,但令人困惑的用户体验,因为网页中的“打印”按钮不执行任何操作。

最佳答案

也许这能回答你的问题: https://bugs.chromium.org/p/chromium/issues/detail?id=322303

这是 Android WebView 团队的答案:

Hi,

We (Android WebView team) are planning to port this API to Android Q (lower versions are possible too via support lib, but TBD). We want to understand the possible use cases of it.

The API will be a blocking callback as we want to freeze the DOM/layout tree for printing rendering, this is the same behavior with Chrome.

Once we have the callback, we can imagine to get a |PrintDocumentAdapter| from WebView#getPrintDocumentAdapter() in the callback, and apps can either 1) use the |PrintDocumentAdapter| with |PrintManager| to let user choose printing settings or 2) use it to generate a PDF file directly. We probably want to unblock when PrintDocumentAdapter#onFinish() get called.

This callback will only benefit the case that when loading contents you have no control to WebView, the app still needs to support printing feature. Otherwise the app can use |PrintDocumentAdapter| directly.

We also want to know if you think window.print() from an iframe (to print only content in that iframe) is important for you or not. Supporting of this is tentative as there is no obvious way for getPrintDocumentAdapter() to know the current window.print() is from main frame or an iframe.

Questions, thoughts? Please give your feedback, thanks!

关于android - Window.Print 在 Android WebView (API 23) 中不起作用,但在默认浏览器和 Chrome 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45810544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com