如何通过单击按钮更改webview的URL?

时间:2014-06-21 14:26:14

标签: java android webview

所以,我对Android开发相当陌生,并决定 - 在最初的一些摆弄之后 - 让我的第一个完整的应用程序成为带导航抽屉的应用程序。理想情况下,如果您在导航抽屉中选择一个项目,我想要一个URL更改的WebView。例如:导航抽屉包括以下列表:Google,Stackoverflow,Facebook和Twitter。如果单击其中一个,则将正确的URL推送到webview。

我开始使用Google提供的代码。带导航抽屉的示例应用程序,带有行星图像。我改变了一些东西,但我目前坚持的步骤是你点击一个项目并且webview改变的那一步。您可以在下面看到代码的一些片段:

fragment_planet.xml

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:gravity="center"
    android:padding="32dp" />

MainActivity

private void selectItem(int position) {
    // update the main content by replacing fragments
    Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

我希望有人可以帮助我,因为我找不到任何文件。

1 个答案:

答案 0 :(得分:0)

loadUrl课程中有Webview个方法。它可以随时调用。只需在PlanetFragment中定义方法,即可在其中调用loadUrl方法。