单击按钮时如何直接移动到pdf文件的特定页码

时间:2011-07-06 09:39:16

标签: android android-layout android-widget android-manifest

我编写了在android应用程序中显示pdf文件的代码。 我想在点击按钮时显示特定页面。请帮助我。

...谢谢

我创建了这样的类

package com.tiru;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SetupForAirport extends ListActivity{

    private List<String> item = null;
    private List<String> path = null;
    private String root = "/";
    private TextView myPath;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.setup_airport);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
                R.layout.window_title_for_setupairport);



        Button buttonDep = (Button) findViewById(R.id.setDep);
        buttonDep.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                //for Samsung tab
                myPath = (TextView) findViewById(R.id.path);
                getDir("/mnt/sdcard/external_sd/doc1/JEPPESEN MANUAL/");    

            }
        });

    }

    private void getDir(String dirPath) {
        myPath.setText("Location: " + dirPath);

        item = new ArrayList<String>();
        path = new ArrayList<String>();

        File f = new File(dirPath);
        File[] files = f.listFiles();

        if (!dirPath.equals(root)) {

            item.add(root);
            path.add(root);

            item.add("../");
            path.add(f.getParent());

        }

        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            path.add(file.getPath());
            if (file.isDirectory())
                item.add(file.getName() + "/");
            else
                item.add(file.getName());
        }

        ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, 
                R.layout.row, item);
        setListAdapter(fileList);
    }

    protected void onListItemClick(ListView l, View v, int position, long id) {

        File file = new File(path.get(position));

        if (file.isDirectory()) {
            if (file.canRead())
                getDir(path.get(position));
            else {
                new AlertDialog.Builder(this)
                        .setIcon(R.drawable.icon)
                        .setTitle(
                                "[" + file.getName()
                                        + "] folder can't be read!")
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        // TODO Auto-generated method stub
                                    }
                                }).show();
            }
        } else {

            //For Reading pdf file
            Uri path = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(path, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(SetupForAirport.this,
                        "No Application Available to View PDF",
                        Toast.LENGTH_SHORT).show();
            }

        }
    }

}

我想,当点击buttonDep时,它将转到pdf文件的第10页

1 个答案:

答案 0 :(得分:0)

您可以采用其他方式:

     Button b;
     b = (Button)findViewById(R.id.button1);
      ed = (EditText)findViewById(R.id.editText1);
        ed.setText("http://static.googleusercontent.com/external_content/untrusted_dlcp/source.android.com/en//compatibility/2.1/android-2.1-cdd.pdf");


    b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             String URL  = ed.getText().toString();
             URL.trim();
             System.out.println(URL);
             WebView web1 =(WebView)findViewById(R.id.webview);
             web1.getSettings().setJavaScriptEnabled(true);
             web1.loadUrl("http://docs.google.com/gview?embedded=true&url="+URL);    


        }
    });