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:
First, let's make sure your app can handle PDFs with special formatting:
- Add PDF files to your project: Create an
assetsfolder in your app'smaindirectory (right alongsidejavaandres). Drop your two PDF files here—name thembutton1.pdfandbutton2.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.gradlefile and add this dependency inside thedependenciesblock:
Sync your project once you've added this.implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'
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>
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 } }
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))); } }
- Double-check that your
assetsfolder is in the right place (it should be underapp/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

