Android-Webview在下一个活动中加载

时间:2013-08-26 10:10:49

标签: android android-webview

我有一个WebView我正在从WebService获取数据。 我的问题是,我得到的数据中有很多url's,当用户点击它时,它应该加载到下一页上的WebView而不是浏览器上。 我怎样才能做到这一点。我试过了shouldOverrideUrlLoading,但它没有用。

1 个答案:

答案 0 :(得分:0)

试试这个。

public class WebViewActivity extends Activity {
    private ProgressDialog progressBar;

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

        progressBar = ProgressDialog.show(this, "Hi", "me");
        WebView webView = (WebView) findViewById(R.id.webView1);

        webView.setWebViewClient(new MyWebViewClient());
        webView.loadUrl("http://www.mysite.com/android.php");
    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageFinished(WebView view, String url) {
            if (progressBar != null && progressBar.isShowing()) {
                progressBar.dismiss();
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            System.out.println("here");


            //Upadated code..........

            Bundle bundle = new Bundle();
            //Add your data from getFactualResults method to bundle
            bundle.putString("NextUrl", url);

            Intent i = new Intent(WebViewActivity.this, NextActivity.class);
            i.putExtras(bundle);
            startActivity(i);

            //-----------------------

            Log.i("Hi", "WEb nextttt");

            return true;
        }
    }
}


 NextActivity 


 public class NextActivity extends Activity {

    WebView wv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        Bundle bundle = getIntent().getExtras();

        //Extract the data…
        String url = bundle.getString("NextUrl");        


        Log.i("Hi", "Get next");
        wv1 = (WebView) findViewById(R.id.webView1);
        wv1.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
        wv1.getSettings().setSupportZoom(false);
        wv1.getSettings().setSupportMultipleWindows(false);
        wv1.setHorizontalScrollBarEnabled(false);
        wv1.setVerticalScrollBarEnabled(false);
        wv1.getSettings().setUseWideViewPort(true);
        wv1.getSettings().setLoadWithOverviewMode(true);
        wv1.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView viewx, String urlx) {
                viewx.loadUrl(urlx);
                return false;
            }
        });
        try {
            wv1.loadUrl(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}