简体   繁体   English

按钮单击以在所需活动中进行网络查看

[英]Button click to web-view in desired activity

I'm in progress on developing an android application and I'm looking for how to implement the following cases.我正在开发一个 android 应用程序,我正在寻找如何实现以下案例。

  1. How to open a web view in second activity by clicking a button in the main activity?如何通过单击主活动中的按钮在第二个活动中打开 Web 视图?
  2. And, How to navigate back to the main activity when the back button is pressed.以及,如何在按下后退按钮时导航回主活动。
    1. I have 12 buttons in the main activity each opens a web view in the second activity.我在主活动中有 12 个按钮,每个按钮在第二个活动中打开一个 Web 视图。 For example button 1 is for google.then by clicking button 1 one can view web view of google in the second activity.Like this button 2 is for Face Book.Then by clicking button 2 one can view web view of Face Book in the second activity.How to Implement this scenario for 12 buttons in the main activity.例如,按钮 1 用于 google。然后通过单击按钮 1 可以在第二个活动中查看 google 的网络视图。像这个按钮 2 用于 Facebook。然后通过单击按钮 2 可以在第二个活动中查看 Facebook 的网络视图活动。如何为主活动中的 12 个按钮实现此方案。

Add the following code in your secondActivity.xml在您的 secondActivity.xml 中添加以下代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

Add following code in onCreate() of your secondActivity.java在 secondActivity.java 的 onCreate() 中添加以下代码

Webview webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.google.com");

Take a button in your firstActivity.xml在你的 firstActivity.xml 中取一个按钮

<Button
        android:text="Open"
        android:id="@+id/btnOpen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

Redirect to secondActivity重定向到第二个活动

btnOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    startActivity(new Intent(getApplicationContext(),SecondActivity.class));
                    finish();

            }
        });

Add this in your onBackPressed将此添加到您的 onBackPressed

@Override
    public void onBackPressed() {
        startActivity(new Intent(getApplicationContext(),FirstActivity.class));
        finish();
    }

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

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