根据按钮链接在webview中显示特定URL

时间:2017-10-15 11:14:06

标签: java android xml android-layout webview

我是Android应用程序开发的新手。通过搜索许多链接并阅读StackOverflow上的讨论,我可以通过按钮链接创建WebView。但是我在WebView中使用多URL并不成功,尽管我已经尝试了很多。我的意思是如何根据特定的按钮链接在一个webview中显示不同的URL 。例如:如果按钮1单击它将显示google.com,按钮2将在webview中显示facebook.com等。我编码如下。

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Loading from a Webpage"
        android:layout_margin="10dp"
        android:onClick="clickweb"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Loading from a Webpage"
        android:layout_margin="10dp"
        android:onClick="clickweb"/>

</LinearLayout>

MainActivity.java

公共类MainActivity扩展了Activity {

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

}
public void clickweb(View v) {
    Toast.makeText(MainActivity.this, "Web view selected", Toast.LENGTH_LONG).show();
    Intent i1 = new Intent(MainActivity.this,LoadWeb.class);
    startActivity(i1);
}
}

LoadWeb.java

@SuppressLint("SetJavaScriptEnabled")
public class LoadWeb extends Activity {

private WebView mWebView;

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

    // Get reference of WebView from layout/webviewex.xml
    mWebView = (WebView) findViewById(R.id.webView1);

    setUpWebViewDefaults(mWebView);
    // Load website
    mWebView.loadUrl("https://www.google.co.in");
}

//Convenience method to set some generic defaults for a

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setUpWebViewDefaults(WebView webView) {
    WebSettings settings = webView.getSettings();

    // Enable Javascript
    settings.setJavaScriptEnabled(true);

    // Use WideViewport and Zoom out if there is no viewport defined
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);

    // Enable pinch to zoom without the zoom buttons
    settings.setBuiltInZoomControls(true);

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
        // Hide the zoom controls for HONEYCOMB+
        settings.setDisplayZoomControls(false);
    }

    // Enable remote debugging via chrome://inspect
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
}
}

webviewex.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

您的按钮需要不同的情况。之后,您可以在开始LoadWeb活动之前添加额外内容。在这种情况下,您将添加一个包含您要加载的URL的String。最后一步是从起始意图中获取LoadWeb活动中的String,并将其设置为loadUrl()中的URL;

<强> MainActivity.java

public void clickweb(View v) {

    Toast.makeText(MainActivity.this, "Web view selected", Toast.LENGTH_LONG).show();
    Intent i1 = new Intent(MainActivity.this,LoadWeb.class);

    switch(v.getId()) {
        case R.id.button1:
            i1.putExtra("URL", "https://www.google.co.in");
            break;
        case R.id.button2:
            il.putExtra("URL", "https://www.facebook.com");
            break;
    }

    startActivity(i1);
}

<强> LoadWeb.java

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

    // Get reference of WebView from layout/webviewex.xml
    mWebView = (WebView) findViewById(R.id.webView1);

    setUpWebViewDefaults(mWebView);

    // Get URL from Intent
    String URL = getIntent().getExtras().getString("URL");

    // Load website
    mWebView.loadUrl(URL);
}

查看Intent的文档,了解Intent可以发送和检索的其他内容 https://developer.android.com/reference/android/content/Intent.html