【问题标题】:Displaying the webpage in a relativelayout using webview使用 webview 在相对布局中显示网页
【发布时间】:2014-02-03 10:03:52
【问题描述】:

大家好,这是我的 xml 代码,我正在尝试在窗口中显示浏览器,但它总是被重定向到浏览器

Xml:

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

代码:

public class Activity_Retailers extends Activity {
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview_activity);
    WebView webview = (WebView)findViewById(R.id.webview);
    WebSettings webSettings = webview.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDatabaseEnabled(true);
    webview.loadUrl("http://www.facebook.com");
    webview.setWebViewClient(new WebViewClient());
}

帮帮我,非常感谢

【问题讨论】:

  • 它的工作代码..你的错误是什么?您是否在清单中指定了 Internet 权限??

标签: android xml webview window


【解决方案1】:

在 java 代码中将 WebViewClient 设置为您的 WebView 并覆盖 shouldOwerrideUrlLoading() 方法。

myWebView.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return true;
    }
});

【讨论】:

    【解决方案2】:

    试试下面的代码,

    public class Main extends Activity {
        private WebView webview;
        private static final String TAG = "Main";
        private ProgressDialog progressBar;  
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            requestWindowFeature(Window.FEATURE_NO_TITLE);
    
            setContentView(R.layout.main);
    
            this.webview = (WebView)findViewById(R.string.webview);
    
            WebSettings settings = webview.getSettings();
            settings.setJavaScriptEnabled(true);
            webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    
            final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    
            progressBar = ProgressDialog.show(Main.this, "WebView Example", "Loading...");
    
            webview.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    Log.i(TAG, "Processing webview url click...");
                    view.loadUrl(url);
                    return true;
                }
    
                public void onPageFinished(WebView view, String url) {
                    Log.i(TAG, "Finished loading URL: " +url);
                    if (progressBar.isShowing()) {
                        progressBar.dismiss();
                    }
                }
    
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Log.e(TAG, "Error: " + description);
                    Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
                    alertDialog.setTitle("Error");
                    alertDialog.setMessage(description);
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
                    alertDialog.show();
                }
            });
            webview.loadUrl("http://www.google.com");
        }
    }
    

    【讨论】:

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