Android打印HTML文档(Android Print HTML Document)

编程入门 行业动态 更新时间:2024-10-28 06:36:32
Android打印HTML文档(Android Print HTML Document)

我是Android开发新手,并坚持使用本文档和本教程

基本上,我想打印一个网页到PDF。 但是当使用文档时,我得到getActivity()函数的错误。

而在第二个链接(教程)中,我得到PlaceholderFragment()错误。

任何人都可以给我一个完整的例子来打印一个HTML页面?

我也想知道一个片段是什么? 我应该创建一个片段来使代码有效吗? 以及如何做到这一点?

I'm new into Android development and stuck with this documentation and this tutorial

Basically, I want to print a web page into a PDF. But when using the documentation I'm getting error with getActivity() function.

And in the second link (the tutorial) I'm getting error with PlaceholderFragment().

Could anyone give me a complete example to print a HTML Page?

I also want to know what a fragment is? Should I create a fragment to make the code work? And how to do that?

最满意答案

Android应用程序的结构包含许多组件:活动,片段,服务,适配器,模型和控制器/业务类。

每个组件都extends了一个特定的android类。 例如,活动扩展了Activity类或AppCompatActivity类。 片段可以扩展Fragment类,服务扩展Service类,等等...

有一些方法只能用于活动,因为它们是Activity类方法(或方兴未艾)。

因此,如果您需要使用getActivity()方法,该方法可以被Fragment使用,但是您不在片段中,则需要以其他方式获取活动。

例如:

private void doWebViewPrint() { WebView webView = new WebView(getActivity()); }

应该成为:

private void doWebViewPrint(Activity activity) { WebView webView = new WebView(activity); }

你必须从一个活动中调用它:

webprinter.doWebViewPrint(this);

甚至更好:

private void doWebViewPrint(Context context) { WebView webView = new WebView(context); }

你必须从一个活动中调用它:

webprinter.doWebViewPrint(getApplicationContext());

在你写的第二个链接中,我找不到任何提及的PlaceholderFragment。

顺便说一下,在您的应用中使用片段不是强制性的,您可以使用活动来制作有效的应用。 碎片对于在活动的同一个容器中管理不同的“视图”很有用,例如使用导航抽屉时,它们有自己的生命周期,并且可以在不更改活动的情况下附加和分离。

您可以在这里找到更多关于如何将HTML打印为PDF的信息 。

An Android App is structured to have many components: activities, fragments, services, adapters, models and your controller/business classes.

Each component extends a specific android class. For example, activities extends Activity class or AppCompatActivity class. Fragment can extend Fragment class, Service extends Service class, and so on...

There are some methods that can only be used in activity because they are Activity class methods (or ascendant).

So if you need to use the getActivity() method, that can be used by a Fragment, but you are not in a fragment, you need to get the activity in some other way.

For example:

private void doWebViewPrint() { WebView webView = new WebView(getActivity()); }

should become:

private void doWebViewPrint(Activity activity) { WebView webView = new WebView(activity); }

and you have to call it from an activity calling:

webprinter.doWebViewPrint(this);

or even better:

private void doWebViewPrint(Context context) { WebView webView = new WebView(context); }

and you have to call it from an activity calling:

webprinter.doWebViewPrint(getApplicationContext());

In the second link you wrote, I can't find any mention to PlaceholderFragment.

By the way, it is not mandatory to use fragments in your app, you can just use activities for having a working app. Fragments are useful for managing different "views" in a same container of an activities, for example when using a navigation drawer, they have their own lifecycle and can be attached and detached without changing activity.

You can find more information on how to Print HTML to PDF here.

更多推荐

本文发布于:2023-08-03 01:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1383118.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文档   HTML   Android   Print   Document

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!