Android之webView入门

WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用。

webview有两个方法:setWebChromeClient 和 setWebClient
setWebClient:主要处理解析,渲染网页等浏览器做的事情
setWebChromeClient:辅助WebView处理Javascript的对话框,网站图标,网站title,加载进度等
WebViewClient就是帮助WebView处理各种通知、请求事件的。
在AndroidManifest.xml设置访问网络权限:
“`

控件:
 ``` <WebView 
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/webView"
    /> 

用途一:加载本地/Web资源

实例

public class MainActivity extends Activity {

    private EditText et_address=null;
    private WebView webview=null;
    private ProgressDialog pd=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_address=(EditText) findViewById(R.id.ed_address);
        webview=(WebView) findViewById(R.id.webview);
        pd=new ProgressDialog(this);
        pd.setMessage("正在加载");

        WebSettings setting=webview.getSettings();
        setting.setSupportZoom(true);
        setting.setBuiltInZoomControls(true);
        webview.setWebViewClient(new WebViewClient()
        {

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                pd.show();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                pd.dismiss();
            }

        }
        );

    }


    public void click(View view)
    {
        String address=et_address.getText().toString().trim();

        if(TextUtils.isEmpty(address))
        {
            address="http://192.168.247.1:8080";
        }

        webview.loadUrl(address);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        if(keyCode==KeyEvent.KEYCODE_BACK&&webview.canGoBack())
        {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub

        menu.addSubMenu(0, 0, 0, "刷新");
        menu.addSubMenu(0, 0, 1, "后退");
        menu.addSubMenu(0, 0, 2, "前进");

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        switch (item.getOrder()) {
        case 0:
            webview.reload();
            break;
        case 1:
            if(webview.canGoBack())
            {
                webview.goBack();
            }else
            {
                Toast.makeText(getApplicationContext(), "已经到第一页了", 0).show();
            }
            break;
        case 2:
            if(webview.canGoForward())
            {
                webview.goForward();
            }else
            {
                Toast.makeText(getApplicationContext(), "已经是最后一页了", 0).show();
            }
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}

参考链接:webView的使用以及总结
http://www.360doc.com/content/14/0611/13/15210553_385676271.shtml

你可能感兴趣的:(android,webView)