You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android新手求助:实现点击按钮跳转至对应Activity离线显示PDF

Hey there! Let's get your offline PDF viewing setup sorted—including proper support for math formulas and smooth navigation between your activities. Here's a straightforward, step-by-step guide tailored to your project:

1. Prep Your PDF Files & Dependencies

First, let's make sure your app can handle PDFs with special formatting:

  • Add PDF files to your project: Create an assets folder in your app's main directory (right alongside java and res). Drop your two PDF files here—name them button1.pdf and button2.pdf (keep filenames simple, no spaces or weird characters).
  • Add a reliable PDF viewer library: We'll use AndroidPdfViewer because it's great at rendering math formulas and supports all the offline viewing features you need. Open your app-level build.gradle file and add this dependency inside the dependencies block:
    implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
    
    Sync your project once you've added this.
2. Update Your PDF Activity Layouts

For both button1_pdf.xml and button2_pdf.xml, replace the default layout with a full-screen PDF viewer component. Here's the code for one (repeat for the other):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
3. Code the PDF Activity Logic

Now let's make the Activity load the correct PDF from your assets. Let's use Button1PDF as an example—copy this pattern for Button2PDF (just swap the PDF filename):

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;

public class Button1PDF extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button1_pdf);

        PDFView pdfView = findViewById(R.id.pdfView);
        // Load the PDF from assets, add useful features
        pdfView.fromAsset("button1.pdf")
                .defaultPage(0) // Start at the first page
                .scrollHandle(new DefaultScrollHandle(this)) // Show scroll position
                .enableSwipe(true) // Allow swiping to flip pages
                .swipeHorizontal(false) // Vertical swiping (set to true for horizontal)
                .enableDoubletap(true) // Double-tap to zoom in/out
                .load(); // Trigger the load
    }
}
4. Add Button Click Navigation in MainActivity

Finally, link your MainActivity buttons to the corresponding PDF Activities. First, make sure your buttons in activity_main.xml have unique IDs (like @+id/btn1 and @+id/btn2). Then add this code to your MainActivity.java:

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn1 = findViewById(R.id.btn1);
        Button btn2 = findViewById(R.id.btn2);

        // Navigate to Button1PDF when btn1 is clicked
        btn1.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, Button1PDF.class)));

        // Navigate to Button2PDF when btn2 is clicked
        btn2.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, Button2PDF.class)));
    }
}
Quick Notes to Avoid Issues
  • Double-check that your assets folder is in the right place (it should be under app/src/main).
  • Your PDF files must be properly formatted—if math formulas look off, verify the PDF itself renders correctly on a computer first.
  • No extra permissions are needed here since we're loading files from the app's own assets (no external storage access required).

内容的提问来源于stack exchange,提问作者Robert Ken Ovoya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 07:54:06