单击按钮在WebView中打开URL

时间:2013-08-13 14:38:09

标签: android url button webview

我正在制作一个应用程序,其中我使用了三个按钮,当按下这些按钮时,应该在webview中加载分配给它们的相应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;
        }
    }
}

myViewClient.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;

    }
}

0 个答案:

没有答案