简体   繁体   English

如何分配每个按钮以从资产文件夹打开pdf文件

[英]How to assign each button to open a pdf file from assets folder

Hi there please my goal is to create an application with five buttons to open five book pdf by each one of them open an different activity . 嗨,我的目标是创建一个具有五个按钮的应用程序,以通过打开每个活动中的每个按钮来打开五本书pdf。

MainActivity class : MainActivity类

public void onClick(View v) {
    switch (v.getId())
    {
        case R.id.btn1:
            x=1;
                Intent i = new Intent(MainActivity.this, Pdf_Activity.class);
                startActivity(i);
            break;
        case R.id.btn2:
            x=2;
            Intent i2=new Intent(MainActivity.this,Pdf_Activity.class);
            startActivity(i2);
            break;
        case R.id.btn3:
            x=3;
            Intent i3=new Intent(MainActivity.this,Pdf_Activity.class);
            startActivity(i3);
            break;
        case R.id.btn4:
            x=4;
            Intent i4=new Intent(MainActivity.this,Pdf_Activity.class);
            startActivity(i4);
            break;
        case R.id.btn5:
            x=5;
            Intent i5=new Intent(MainActivity.this,Pdf_Activity.class);
            startActivity(i5);
            break;
            default:
                break;
    }

I want to open a pdf file within buttons and this is "pdf_Activity" : 我想在按钮内打开一个pdf文件,这是“ pdf_Activity”:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.content_pdf);
   switch (main.x)
   {
       case (1):
           pdf1=(PDFView) findViewById(R.id.pdf1);
           pdf1.fromAsset("insani.pdf").load();
           break;
       case (2):
           pdf2=(PDFView) findViewById(R.id.pdf2);
           pdf2.fromAsset("kawniya.pdf").load();
           break;
       case (3):
           pdf3=(PDFView) findViewById(R.id.pdf3);
           pdf3.fromAsset("3ilm.pdf").load();
           break;
       case (4):
           pdf4=(PDFView) findViewById(R.id.pdf4);
           pdf4.fromAsset("9iyam.pdf").load();
           break;
       case (5):
           pdf5=(PDFView) findViewById(R.id.pdf5);
           pdf5.fromAsset("a5la9.pdf").load();
           break;
           default:
               break;
   }

I think there are some problems in these two code . 我认为这两个代码存在一些问题。 please help 请帮忙

I would change the switch in the onClick to : 我会将onClick的开关更改为:

Intent i = new Intent(MainActivity.this, Pdf_Activity.class);
switch (v.getId())
{
    case R.id.btn1:
        i.putExtra("file",1);
            startActivity(i);
        break;
    case R.id.btn2:
        i.putExtra("file",2);
        startActivity(i);
        break;
    case R.id.btn3:
        i.putExtra("file",3);
        startActivity(i);
        break;
    case R.id.btn4:
        i.putExtra("file",4);
        startActivity(i);
        break;
    case R.id.btn5:
        i.putExtra("file",5);
        startActivity(i);
        break;
        default:
            break;
}

Then in the pdf_activity Use it like so: 然后在pdf_activity像这样使用它:

Bundle bundle = getIntent().getExtras();
int x = bundle.getInt("file");

switch (x)
{
...
}

I think the problem is in the XML file of pdf files 我认为问题出在pdf文件的XML文件中

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
<com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdf1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
<com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdf2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
<com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdf3"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
.......................
....................
</LinearLayout>

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM