Android 使用按钮在webview中以不同片段打开网站

Android 使用按钮在webview中以不同片段打开网站,android,android-studio,android-webview,android-button,Android,Android Studio,Android Webview,Android Button,我在不同的片段1中有2个按钮,在片段2中有另一个webview。每当我点击按钮时,我想在webview中打开两个不同的网站。请建议请试试这个- public class MainHomeActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_first, btn_second; @Override protected void onCreate(@N

我在不同的片段1中有2个按钮,在片段2中有另一个webview。每当我点击按钮时,我想在webview中打开两个不同的网站。请建议

请试试这个-

public class MainHomeActivity extends AppCompatActivity implements  View.OnClickListener {

    private Button btn_first, btn_second;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_home);

        btn_first= (Button)findViewById(R.id.btn_first);
        btn_second= (Button)findViewById(R.id.btn_second);
        btn_second.setOnClickListener(this);
        btn_first.setOnClickListener(this);
    }

    private void loadFragment(String url) {
        WebViewFragment webViewFragment = new WebViewFragment();
        Bundle b = new Bundle();
        b.putString("url", url);
        webViewFragment.setArguments(b);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment_layout, webViewFragment);
        fragmentTransaction.commit();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_first:
                loadFragment("http://www.google.com");
                break;
            case R.id.btn_second:
                loadFragment("http://www.apple.com");
                break;
        }

    }
}
/活动\u main\u home.xml/

//activity_main.xml

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

    <ProgressBar
        android:id="@+id/webViewPbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal" />

    <WebView android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>

/androidmanifest文件/


只需使用
setArguments
getArguments
public class WebViewFragment extends Fragment {

    private View view;
    private WebView webview;
    ProgressBar webViewPbar;
    private String url ;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        url = bundle.getString("url");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_main, container, false);
        initUI();
        return view;

    }

    private void initUI() {
        webViewPbar = (ProgressBar) view.findViewById(R.id.webViewPbar);

        webview = (WebView) view.findViewById(R.id.webview);
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);
        webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);

                webViewPbar.setProgress(newProgress);
                if (newProgress == 100) {
                    webViewPbar.setVisibility(View.GONE);
                }
            }
        });

        webview.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            public void onPageFinished(WebView view, String url) {
                if (webViewPbar != null) {
                    webViewPbar.setVisibility(View.GONE);
                }
            }

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                if (webViewPbar != null) {
                    webViewPbar.setVisibility(View.GONE);
                }
            }
        });

//        webview.loadUrl("http://www.google.com");
        webview.loadUrl(url);

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

    <ProgressBar
        android:id="@+id/webViewPbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        style="@android:style/Widget.Holo.Light.ProgressBar.Horizontal" />

    <WebView android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shajib.capturemirror">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        >
        <activity android:name=".MainHomeActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>