0

我在我的第一个活动中有两个按钮说 A,我在第二个活动中有一个 webview 说 B 现在我想在按钮点击事件的 webview 中加载 url。如果我点击 btn1 按钮,那么 webview 应该显示 google.com 网站,如果我点击 btn2 按钮,那么 webview 应该显示 gmail.com 网站,这是我的 A 和 B 活动代码。提前致谢

活动 A 代码

 Button btn1 = (Button) findViewById(R.id.btn_google);
            btn1.setOnClickListener(new OnClickListener() {
                 public void onClick(View v) {
                     Intent intent = new Intent (google.this,webview.class);
                     google.this.startActivity(intent);

               }
                }

                    );

             Button btn2 = (Button) findViewById(R.id.btn_gmail);
             btn_signup.setOnClickListener(new OnClickListener() {
                 public void onClick(View v) {

                    Intent intent = new Intent (google.this,webview.class);
                    google.this.startActivity(intent);

               }
                }


                );

这是活动B的代码

super.onCreate(savedInstanceState);
          setContentView(R.layout.webview );

           WebViewClient yourWebClient = new WebViewClient()
           {
               // Override page so it's load on my view only
               @Override
               public boolean shouldOverrideUrlLoading(WebView  view, String  url)
               {
                // This line we let me load only pages inside Firstdroid Webpage
                 view.loadUrl(url);
                   // Load new URL Don't override URL Link

                // Return true to override url loading (In this case do nothing).
                return true;
               }
           };


           // Get Web view
           mWebView = (WebView) findViewById( R.id.mywebview ); //This is the id you gave 
           mWebView.getSettings().setJavaScriptEnabled(true);   
           mWebView.getSettings().setSupportZoom(true);       //Zoom Control on web (You don't need this 
                                                  //if ROM supports Multi-Touch      
           mWebView.getSettings().setBuiltInZoomControls(true); //Enable Multitouch if supported by ROM
           mWebView.setWebViewClient(yourWebClient);
           mWebView.reload();

}
4

2 回答 2

0

我没有清楚地理解您的问题,但是要从 webview 的任何位置加载 url,请执行以下操作:

mwebview.loadUrl(YOUR_URL);

如果您想将 url 从 A 活动传递到 B 活动,请使用

intent.putExtra(KEY,VALUE)

希望这可以帮助

于 2012-06-27T16:47:33.080 回答
0

您必须为 btn1 执行此操作

 Intent intent = new Intent (google.this,webview.class);
intent.putExtra("google.com", "http://www.google.com");
                     google.this.startActivity(intent);

您必须为 btn2 执行此操作

 Intent intent = new Intent (google.this,webview.class);
intent.putExtra("gmail.com", "http://www.gmail.com");
                     google.this.startActivity(intent);

在活动2

Bundle extras = getIntent().getExtras();
if (extras != null){
String googleUrl = extras.getString("google.com");
String gmailUrl = extras.getString("gmail.com");
if (googleUrl != null)
    mWebView.loadUrl(googleUrl);
else if (gmailUrl != null)
 mWebView.loadUrl(gmailUrl);
}
于 2012-06-27T18:02:57.037 回答