首页>代码>Android学习开发的个人简单APP>/bolo/src/com/example/bolo/browser.java
package com.example.bolo;

import com.example.myapp.R;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.ActionMode.Callback;
import android.view.KeyEvent;
import android.view.View;
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.Toast;
 
public class browser extends Activity {
 
    private String url = null;
    private WebView webView;
    private ProgressDialog dialog;
    private EditText text;
    private Button button;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.browser);
        Toast.makeText(this,"欢迎使用菠萝浏览器!",Toast.LENGTH_SHORT).show(); //弹出欢迎
        init();
    }
    
    private void init() {
        webView = (WebView) findViewById(R.id.webview);
        text = (EditText) findViewById(R.id.text);
        button = (Button) findViewById(R.id.button);
        webView.loadUrl(url);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str = text.getText().toString();  //去获取text中输入的网址
                url = "http://"+ str;
                webView.loadUrl(url);            //设置到webView中去
            }
        });
 
 
 
        //覆盖WebView默认通过第三方或者是系统浏览器打开网页的行为,使网页可以再WebView中打开
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //返回值是true的时候控制网页在WebView中去打开,如果为false调用系统浏览器或者第三方浏览器打开
 
                view.loadUrl(url);
                return true;
            }//WebViewClient帮助WebView去处理一些页面控制和请求通知
        });
 
        //启用支持javaScript
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        //WebView加载页面优先使用缓存加载
        settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                //newProgress 1-100之间的整数
                if (newProgress == 100) {
                    //网页加载完毕,关闭ProgressDialog
                    closeDialo();
                } else {
                    //网页正在加载,打开ProgressDialog
                    openDialog(newProgress);
                    text.setText(webView.getUrl()); //实时显示当前网址
                    text.requestFocus();           //把输入焦点放在调用这个方法的控件上
                    text.setSelectAllOnFocus(true); //点击之后就被全选
                }
            }
 
            private void closeDialo() {
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                    dialog = null;
                }
            }
 
            private void openDialog(int newProgress) {
                if (dialog == null) {
                    dialog = new ProgressDialog(browser.this);
                    dialog.setTitle("加载中...");
                    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    dialog.setProgress(newProgress);
                    dialog.show();
                } else {
                    dialog.setProgress(newProgress);
                }
            }
        });
 
    }
 
    @Override //改写物理按键――返回的逻辑
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            if(webView.canGoBack()){
                webView.goBack();   //返回上一页面
                return true;
            }else {
                System.exit(0);
            }
        }
        return super.onKeyDown(keyCode,event);
    }
 
}
最近下载更多
不止是六位数  LV7 2023年12月23日
学习112  LV2 2023年12月21日
hhhhhz  LV7 2023年11月14日
xxxdragon  LV10 2023年7月27日
yyyyyyzh  LV8 2023年6月11日
200171  LV9 2023年6月1日
sks666  LV5 2023年2月19日
yzhszz  LV3 2022年12月27日
我是大帅哥  LV11 2022年12月14日
廖乐儿  LV2 2022年12月6日
最近浏览更多
zolscy  LV12 3月20日
eagerwujin  LV4 3月9日
tdcq123  LV14 3月8日
yerwiu  LV10 1月6日
暂无贡献等级
微信网友_6801903754432512 2023年12月31日
暂无贡献等级
666ing  LV2 2023年12月27日
wangxiaolaugh  LV2 2023年12月25日
学习112  LV2 2023年12月21日
不止是六位数  LV7 2023年12月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友