在同一活动的webview中打开pdf文件

问题描述 投票:0回答:1

即时通讯创建一个应用程序,它包含pdf文件....当我点击按钮时,pdf文件应该在相同的活动中打开...但是pdf在另一个活动中打开... plz在这方面帮助我.. 。

public class plp extends AppCompatActivity {
Button button,b2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_plp);
    final Context context= this;

    Button button= (Button) findViewById(R.id.button98);
    Button b2=(Button) findViewById(R.id.button99);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i=new Intent(context,plpwebview.class);
            startActivity(i);

        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(context,plpwebview2.class);
            startActivity(i);
        }
    });
}}

公共类plpwebview扩展AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_plpwebview);

    final WebView webView;
    webView=(WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);



    webView.loadUrl("https://drive.google.com/open?id=1XNS4jaQUBflb4nY4Mx7kgxnm3u--tKY9");
}

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/images"
tools:context="com.example.saitarun.git.plp">


<Button
    android:id="@+id/button98"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/unit_i" />


<Button
    android:id="@+id/button99"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/unit_ii" />

<Button
    android:id="@+id/button100"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/unit_iii" />

<Button
    android:id="@+id/button101"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/unit_iv" />

<Button
    android:id="@+id/button102"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/unit_vi" />

android webview
1个回答
0
投票

当我点击按钮时,pdf文件应该在同一个活动中打开...但是pdf在另一个活动中打开

您不需要Intent来显示Google Doc。当你点击它们时,有两个按钮会改变URL,我相信你可以把它编辑到其他网址:)

public class MainActivity extends AppCompatActivity {
    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });

        webView.loadUrl("https://drive.google.com/open?id=1XNS4jaQUBflb4nY4Mx7kgxnm3u--tKY9");

        Button button = (Button) findViewById(R.id.button98);
        Button b2 = (Button) findViewById(R.id.button99);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                webView.loadUrl("https://www.google.com");
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("https://stackoverflow.com/");

            }
        });

    }

}

请记住在布局中添加Webview

<WebView
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>
© www.soinside.com 2019 - 2024. All rights reserved.