我正在制作一个应用程序,其中我使用了三个按钮,当按下这些按钮时,它们应该在 web 视图中加载分配给它们的各自 url,但我无法这样做。
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
public class Webview extends Activity implements OnClickListener {
WebView myweb;
Button bscore, bnews, bschedule;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
bscore= (Button) findViewById(R.id.buttonscore);
bnews=(Button) findViewById(R.id.buttonnews);
bschedule= (Button) findViewById(R.id.buttonschedule);
myweb= (WebView) findViewById(R.id.webView1);
myweb.getSettings().setSupportZoom(true);
myweb.getSettings().setJavaScriptEnabled(true);
myweb.getSettings().setUseWideViewPort(true);
myweb.setWebViewClient(new ourViewClient());
bscore.setOnClickListener(this);
bnews.setOnClickListener(this);
bschedule.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.buttonscore:
myweb.loadUrl("http://m.cricketworld.com/live-scores/");
break;
case R.id.buttonnews:
myweb.loadUrl("http://m.cricketworld.com/live-news/");
break;
case R.id.buttonschedule:
myweb.loadUrl("http://m.cricketworld.com/live-schedule/");
break;
}
}
}
ourViewClient.java 代码如下
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ourViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView v, String url){
v.loadUrl(url);
return true;
}
}