2012-11-22 93 views
-2

我正在設計一個webview。點擊不同的按鈕並顯示不同的內容。就像這樣:關於android界面設計

enter image description here

我想知道,函數,當我切換在左邊的四個按鈕。它可以在右側顯示不同的內容。 謝謝!

+3

你做了什麼做到這一點! – Nermeen

回答

0

你有你的webview和按鈕在你的xml文件。在youractivityname.java試試下面的代碼

public class yourActivityname extends Activity implements OnClickListener { 

    private WebView yourWebView;  
    private Button button1, button2; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
      // assign reference of xml activity contents to your class variables 
     yourWebView = (WebView) findViewById(R.id.web); 
      button1 = (Button) findViewById(R.id.button1); 
      button2 = (Button) findViewById(R.id.button2); 
     String url = "https:/www.google.com"; 
     yourWebView.getSettings().setJavaScriptEnabled(true); 
      //load default url 
      loadWebContents(url); 

    } 

創造出加載網頁的方法,並把這種methon的onclick

private void loadWebContents(String url){ 
     yourWebView.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 

      } 
     }); 

     yourWebView.loadUrl(url); 
    } 

在點擊方法

public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.button1: 
      loadWebContents("www.msn.com"); 
      break; 
     case R.id.restoreBtn: 
      loadWebContents("www.gmail.com"); 
      break; 
     } 

    } 
}