在buttonclick下使用webview从url显示pdf

时间:2018-03-09 14:39:31

标签: android pdf webview

您好,我想从网址添加我的pdf文件。当我添加一些按钮并在onclick下放入webview时,我的项目没有用。但我把webview放在oncreate方法下它运行良好。每个pdf文件创建java.class是否必要?不是可以添加像我这样的按钮吗?我将webview设置如下,但它不起作用,这是错误的感谢:

public class MainActivity extends AppCompatActivity {
WebView webview;
ProgressBar progressbar;
Button ahi, edi, ravm, ard, ox, cr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ahi = (Button) findViewById(R.id.ahi);
    ravm = (Button) findViewById(R.id.ravm);
    edi = (Button) findViewById(R.id.edi);
    cr = (Button) findViewById(R.id.cr);
    ard = (Button) findViewById(R.id.ard);
    ox = (Button) findViewById(R.id.ox);
    webview = (WebView) findViewById(R.id.webview);
    progressbar = (ProgressBar) findViewById(R.id.progressbar);
    webview.getSettings().setJavaScriptEnabled(true);

    ard.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String filename = "http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
            webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);

            webview.setWebViewClient(new WebViewClient() {

                public void onPageFinished(WebView view, String url) {
                    // do your stuff here
                    progressbar.setVisibility(View.GONE);
                }
            });
        }
    });
    ox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String filename = "http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
            webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);

            webview.setWebViewClient(new WebViewClient() {

                public void onPageFinished(WebView view, String url) {
                    // do your stuff here
                    progressbar.setVisibility(View.GONE);
                }
            });
        }
    });

    ahi.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String filename = "http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
            webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);

            webview.setWebViewClient(new WebViewClient() {

                public void onPageFinished(WebView view, String url) {
                    // do your stuff here
                    progressbar.setVisibility(View.GONE);
                }
            });
        }
    });
    edi.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String filename = "http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
            webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);

            webview.setWebViewClient(new WebViewClient() {

                public void onPageFinished(WebView view, String url) {
                    // do your stuff here
                    progressbar.setVisibility(View.GONE);
                }
            });
        }
    });
    ravm.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String filename = "http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
            webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);

            webview.setWebViewClient(new WebViewClient() {

                public void onPageFinished(WebView view, String url) {
                    // do your stuff here
                    progressbar.setVisibility(View.GONE);
                }
            });
        }
    });
    cr.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String filename = "http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
            webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);

            webview.setWebViewClient(new WebViewClient() {

                public void onPageFinished(WebView view, String url) {
                    // do your stuff here
                    progressbar.setVisibility(View.GONE);
                }
            });
        }
    });
}

我的意思是当我在下面设置代码时它工作得很好。但是我想把它放在buttonclick下面,因为它不起作用。我怎么能设置我的高级代码?

public class giris extends AppCompatActivity {
 WebView webview;
ProgressBar progressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_giris);
    webview = (WebView)findViewById(R.id.webview);
    progressbar = (ProgressBar) findViewById(R.id.progressbar);
    webview.getSettings().setJavaScriptEnabled(true);
        String filename = "http://www3.nd.edu/~cpoellab/teaching/cse40816/android_tutorial.pdf";
    webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);
    //String filename ="https://interactivepdf.uniflip.com/2/34834/312285/pub/document.pdf";
    //webview.loadUrl("http://docs.google.com/gview?embedded=true&url=" + filename);
    webview.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
            // do your stuff here
            progressbar.setVisibility(View.GONE);
        }
    });

}

0 个答案:

没有答案