Android开发:如何实现五个按钮分别打开assets文件夹中对应PDF文件?
我来帮你搞定这个需求!你现在的代码只是跳转到Pdf_Activity,但没把对应PDF的关键信息传过去,只需要补上参数传递和PDF加载的逻辑就行,具体步骤如下:
实现步骤
1. 优化MainActivity的点击事件,传递PDF文件名
先把你原来重复的Intent创建代码简化,直接通过Intent的Extra传递对应按钮要打开的PDF文件名,这样不用依赖全局变量x,也更可靠(避免快速点击导致变量被覆盖的问题):
public void onClick(View v) { String targetPdf = ""; switch (v.getId()) { case R.id.btn1: targetPdf = "book1.pdf"; // 对应assets里的第一个PDF文件名,要和实际文件名完全一致 break; case R.id.btn2: targetPdf = "book2.pdf"; break; case R.id.btn3: targetPdf = "book3.pdf"; break; case R.id.btn4: targetPdf = "book4.pdf"; break; case R.id.btn5: targetPdf = "book5.pdf"; break; } // 统一创建Intent并传递参数 Intent pdfIntent = new Intent(MainActivity.this, Pdf_Activity.class); pdfIntent.putExtra("SELECTED_PDF", targetPdf); startActivity(pdfIntent); }
2. 准备assets文件夹和PDF文件
确保你的项目src/main/assets文件夹下存在对应文件名的PDF文件(比如book1.pdf到book5.pdf),文件名要和上面代码里的完全一致,包括大小写。
3. 实现Pdf_Activity加载指定PDF
这里推荐使用成熟的第三方PDF查看库AndroidPdfViewer,它兼容性更好,功能也更完善:
第一步:添加依赖
在你的Module级build.gradle文件中添加依赖:
dependencies { implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1' }
第二步:创建Pdf_Activity的布局
在res/layout/activity_pdf.xml中添加PDFView组件:
<?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/pdf_view" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
第三步:编写Pdf_Activity的逻辑
接收MainActivity传递的PDF文件名,然后从assets加载对应的文件:
import android.content.Intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import com.github.barteksc.pdfviewer.PDFView; import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle; public class Pdf_Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pdf); PDFView pdfView = findViewById(R.id.pdf_view); Intent receivedIntent = getIntent(); String pdfFileName = receivedIntent.getStringExtra("SELECTED_PDF"); // 检查文件名是否有效,然后加载PDF if (pdfFileName != null && !pdfFileName.isEmpty()) { pdfView.fromAsset(pdfFileName) .defaultPage(0) // 默认打开第一页 .scrollHandle(new DefaultScrollHandle(this)) // 添加滚动进度条 .enableSwipe(true) // 允许左右滑动翻页 .load(); } } }
备选方案:用WebView加载PDF(不依赖第三方库)
如果你不想用第三方库,也可以用WebView实现,但注意部分设备可能需要Google Play服务支持才能正常显示PDF:
布局里替换成WebView:
<WebView android:id="@+id/web_view" android:layout_width="match_parent" android:layout_height="match_parent"/>
Pdf_Activity的代码:
import android.content.Intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.webkit.WebSettings; import android.webkit.WebView; public class Pdf_Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pdf); WebView webView = findViewById(R.id.web_view); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); // 启用JS支持 Intent receivedIntent = getIntent(); String pdfFileName = receivedIntent.getStringExtra("SELECTED_PDF"); if (pdfFileName != null && !pdfFileName.isEmpty()) { // 加载assets里的PDF文件 webView.loadUrl("file:///android_asset/" + pdfFileName); } } }
内容的提问来源于stack exchange,提问作者walidroid
相关产品推荐
相关产品推荐

