2012-06-27 12 views
0

我有我的第一個活動中的兩個按鈕說A和我有我的第二個活動中的網頁視圖B 現在我想加載網頁視圖上的按鈕點擊事件如果我點擊btn1按鈕,那麼webview應該顯示google.com網站,如果我點擊btn2按鈕,那麼webview應該顯示gmail.com網站,這裏是我的A和B的活動代碼。在此先感謝如何在按鈕點擊第二個活動的網頁視圖中加載網址

發生的活性

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(); 

} 

回答

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); 

In activity2

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); 
} 
+0

感謝偉大的答案,我希望這會幫助我:) –

0

我沒有清楚地理解你的問題,而是從網頁視圖隨時隨地加載網址這樣做:

mwebview.loadUrl(YOUR_URL); 

如果你想從一個活動傳遞的網址,以B活性使用

intent.putExtra(KEY,VALUE) 

希望這有助於

+0

感謝快速回復,假設我有兩個按鈕,正如我上面提到的。現在我想要當我點擊btn1然後webview應該加載谷歌網頁像mwebview.loadurl(「google.com」),當我點擊btn2那麼mwebview應該加載gmail網頁在像mwebview.loadurl(「gmail.com」)相同的webview,但在活動乙。活動B有一個xml文件,我宣佈mwebview –

相關問題