WebView

来源:互联网 发布:行情分析软件 编辑:程序博客网 时间:2024/06/01 08:21
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:hint="请输入网址"
        android:ems="10" >
    </EditText>

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:text="搜索" />

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class MainActivity extends Activity {    
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        find();
        WebSettings webSettings = webview.getSettings();
        // load_default 只要有缓存,就用缓存
        // load_cache_else_network 当缓存失效时,加载网络
        // LOAD_DEFAULT, LOAD_CACHE_ELSE_NETWORK, LOAD_NO_CACHE or
        // LOAD_CACHE_ONLY. The default value is LOAD_DEFAULT.
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        // 设置是否展示一个缩放的控制器
        webSettings.setBuiltInZoomControls(true);
        // 最高缩放的级别
        webSettings.setDefaultZoom(ZoomDensity.FAR);
        // 设置默认的字体大小
        webSettings.setDefaultFontSize(20);
        // 设置只在当前页面展示
        // webview.setWebViewClient(new WebViewClient());
        // 设置有关网页加载进度的客户端
        webview.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress);
                //显示进度条
                progressBar1.setVisibility(View.VISIBLE);
                progressBar1.setProgress(newProgress);
                //当进度==100,隐藏进度条
                if(newProgress==100){
                    progressBar1.setVisibility(View.GONE);
                }
            }
        });
        button1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String trim = editText1.getText().toString().trim();
                webview.loadUrl(trim);
            }
        });
    }
    private void find() {
        button1=(Button)findViewById(R.id.button1);
        editText1=(EditText)findViewById(R.id.editText1);
        webview=(WebView)findViewById(R.id.webview);
        progressBar1=(ProgressBar)findViewById(R.id.progressBar1);
    }
    //返回键监听
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            //如果webview可以后退
            if(webview.canGoBack()){
                webview.goBack();
            }else{
                finish();
            }
        }
        return true;
    }

0 0