-2

我正在设计一个网页视图。单击不同的按钮并显示不同的内容。像这样:

在此处输入图像描述

当我切换左侧的四个按钮时,我想实现该功能。它可以在右侧显示不同的内容。谢谢!</p>

4

1 回答 1

0

您的 xml 文件中有您的 webview 和按钮。在 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);

    }

创建一个加载您的网络并调用此方法 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;
        }

    }
}
于 2012-11-22T09:52:00.563 回答