按下按钮时加载不同的webView

时间:2014-03-23 21:08:37

标签: android

我会加载不同的webView,以便通过下一个和上一个按钮进行更改,如:

webview.loadUrl("http://developer.android.com/");

并按下下一个按钮

 webview.loadUrl("http://google.com/");

如果按下上一个按钮则返回第一个按钮。

有办法做到这一点吗?

这是适合我的代码

String[] urls = {"http://developer.android.com/", "http://google.com/"};
 int currentPage = 0;

 public void next(View view){ 
 try {
     if (currentPage != urls.length - 1){
         currentPage++;
         wv = (WebView) findViewById(R.id.webview);
         wv.loadUrl(urls[currentPage]);

     }
} catch (Exception e) {

}
 }

1 个答案:

答案 0 :(得分:0)

当然可以。您应该在activity中实现一些方法,并在XML android:onClick中执行它:

Activity

 String ulrs = String[]{"http://developer.android.com/", "http://google.com", (some other)};
 int currentPage = 0;

 public void next(View view){
     if (currentPage != urls.length - 1){
         currentPage++;
         webview.loadUrl(urls[currentPage]);
     }
 }

在布局XML中:

 <Button
  android:id="+@id/next_button"
  (some layout code)
  android:onClick="next" />

上一个按钮代码类似。

button tutorial

相关问题