【问题标题】:How to display a webpage in android?如何在android中显示网页?
【发布时间】:2012-12-05 14:46:49
【问题描述】:

如果我运行应用程序,我需要直接打开网页。 无需在其中使用单个 a 组件。

【问题讨论】:

  • 你不想使用WebView ?
  • 它显示网页在模拟器中不可用。
  • 您是否将<uses-permission android:name="android.permission.INTERNET" /> 添加到您的清单文件中
  • 我添加了另一个使用 WebView 的答案

标签: android web android-webview


【解决方案1】:

这里是直接打开google.com的Activity的onCreate方法:

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

    WebView wb = (WebView) findViewById(R.id.webView1);

    wb.loadUrl("http://www.google.com.tr");
}

这里是activity_browser.xml布局文件:

<WebView 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"
    tools:context=".BrowserActivity"
    android:id="@+id/webView1" > 
</WebView>

别忘了在 AndroidManifest.xml 文件中添加上网权限:

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

【讨论】:

    【解决方案2】:

    在您的 onCreate 方法中创建并启动意图(当然使用 uri.parse)。

    【讨论】:

      【解决方案3】:

      我不太明白,但我认为这就是你需要的......

      Four different ways of opening a web page in Android

      【讨论】:

      • 在回复中加入链接并不是最好的回答方式。如您所见,链接已过期。
      【解决方案4】:

      正如 Ercan 所说,它是这样完成的:

      String url = "http://www.example.com";
      Intent i = new Intent(Intent.ACTION_VIEW);
      i.setData(Uri.parse(url));
      startActivity(i);
      

      在您的 Activity 的 onCreate 方法上执行此操作。

      【讨论】:

        【解决方案5】:

        在 onCreate() 中添加代码

            webView.setWebViewClient(new WebViewClient());    //the lines of code added
            webView.setWebChromeClient(new WebChromeClient()); //same as above
            webView.getSettings().setJavaScriptEnabled(true);
          webView.canGoBack();
        
            webView.loadUrl("your url");
        
        
            final ProgressDialog progressBar = new ProgressDialog(getActivity());
            progressBar.setMessage("Please wait...");
        
        
            webView.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
        
                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    super.onPageStarted(view, url, favicon);
                    if (!progressBar.isShowing()) {
                        progressBar.show();
                    }
                }
        
                public void onPageFinished(WebView view, String url) {
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }
        
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }
            });
        
        
            //To handle Webpage back in fragment
            webView.setOnKeyListener(new View.OnKeyListener() {
        
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK
                            && event.getAction() == MotionEvent.ACTION_UP
                            && webView.canGoBack()) {
                        webView.goBack();
                        return true;
                    }
                    return false;
                }
            });
        

        在xml中添加webview:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-02-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多