android --webview控件 浏览器制作

activity.xlm

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/editText1"
            android:layout_weight="5"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="登陆"
            android:textSize="20dp"
            android:textStyle="bold"
            android:id="@+id/button5"
            android:textColor="#000000"
            android:layout_weight="1"/>
    </LinearLayout>
    <WebView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/web1"
        android:layout_weight="16.5"></WebView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="2">
        <Button
            android:layout_width="wrap_content"
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:text="←"
            android:textColor="#000000"
            android:textStyle="bold"
            android:layout_weight="1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:text="主页"
            android:textStyle="bold"
            android:textColor="#000000"
            android:layout_weight="1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button3"
            android:text="→"
            android:textStyle="bold"
            android:textColor="#000000"
            android:layout_weight="1"/>
       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="1"
           android:text="exit"
           android:textStyle="bold"
           android:textColor="#000000"
           android:id="@+id/button4"/>
    </LinearLayout>


</LinearLayout>

Mainactivity.java

package com.example.wuzuo.web_web;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private WebView webView;
    private String url=null;
    private Button button_1;
    private Button button_2;
    private Button button_3;
    private Button button_4;
    private Button button_5;
    private EditText editText_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);
        setProgressBarIndeterminate(true);
        editText_1= (EditText) findViewById(R.id.editText1);
        button_1= (Button) findViewById(R.id.button1);
        button_2= (Button) findViewById(R.id.button2);
        button_3= (Button) findViewById(R.id.button3);
        button_4= (Button) findViewById(R.id.button4);
        button_5= (Button) findViewById(R.id.button5);
        webView= (WebView) findViewById(R.id.web1);
        button_1.setOnClickListener(this);
        button_2.setOnClickListener(this);
        button_3.setOnClickListener(this);
        button_4.setOnClickListener(this);
        button_5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.button5:
                url="http://"+editText_1.getText().toString();//获取editText当前网址
                url=url.replace(" ","");//网址加上引号
                startreadurl(url);
                break;
            case R.id.button1:
                if(webView.canGoBack())
                {
                    webView.goBack();
                } else {
                    finish();
                }
                break;
            case R.id.button2:
               startreadurl("http://www.baidu.com");
                break;
            case R.id.button3:
                if(webView.canGoForward())
            {
                webView.goForward();
            }
                break;
            case R.id.button4:
                finish();
                break;
        }

    }

    private void startreadurl(String url) {
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {//直接在页面打开,不利用第三方浏览器
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        /*启动javascrip功能*/
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // web加载页面优先使用缓存加载
        webView.setWebChromeClient(new WebChromeClient()//页面加载时有进度显示
        {
                                       @Override
                                       public void onProgressChanged(WebView view, int newProgress) {
                                           setTitle("页面加载" + newProgress + "%");
                                           if(newProgress==100)
                                           {
                                               closeprogressBar();
                                           }
                                           else
                                           {
                                               openprogressBar(newProgress);
                                           }
                                           super.onProgressChanged(view, newProgress);
                                       }
        });
    }
    protected void  openprogressBar(int x)//进度条关闭
    {
        setProgressBarIndeterminateVisibility(true);
        setProgress(x);
    }
    protected void closeprogressBar()//进度条打开
    {
       setProgressBarIndeterminateVisibility(false);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) { // 改写物理按键 返回键的逻辑
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if(webView.canGoBack())
            {
                webView.goBack();
                return  true;
            }
            else
            {
               finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值