android WebView

package com.tinywsn.helloandroid;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.app.AlertDialog;

public class MainActivity extends AppCompatActivity {

    private WebView m_webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        m_webView = (WebView)findViewById(R.id.webview);
        m_webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口,如window.open(),默认为false
        m_webView.getSettings().setJavaScriptEnabled(true);//是否允许执行js,默认为false。设置true时,会提醒可能造成XSS漏洞
        m_webView.getSettings().setSupportZoom(true);//是否可以缩放,默认true
        m_webView.getSettings().setBuiltInZoomControls(true);//是否显示缩放按钮,默认false
        m_webView.getSettings().setUseWideViewPort(true);//设置此属性,可任意比例缩放。大视图模式
        m_webView.getSettings().setLoadWithOverviewMode(true);//和setUseWideViewPort(true)一起解决网页自适应问题
        m_webView.getSettings().setAppCacheEnabled(true);//是否使用缓存
        m_webView.getSettings().setDomStorageEnabled(true);//DOM Storage

        m_webView.loadUrl("https://m.baidu.com");
        m_webView.setWebViewClient( new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ( keyCode == KeyEvent.KEYCODE_BACK  ) {
            if( m_webView.canGoBack() ){
                m_webView.goBack();
                return  true;
            }
            else{
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Android Studio (WebView)");
            builder.setMessage("Hello,Android!");
            builder.setPositiveButton("确定",null);
            builder.show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

 

posted @ 2017-07-15 23:49  未命名blogs  阅读(190)  评论(0编辑  收藏  举报