写个简易的Android WebView App

为了更新和维护方便,大多app开发采取了hybrid模式,前端页面有时在浏览器展现的效果跟在webview里面展现的效果不一样,为了方便测试,自己鼓捣了2天Android开发,弄了个简易的app,兼容(4.4+)功能就是输入网址,在webview里面打开。其它没了。apk下载地址:MyWebViewApp v1.0.1

public class MainActivity extends Activity {

    private String initURL="http://www.baidu.com/";
    private WebView webView;
    private EditText urlText;
    private ProgressDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        webView=(WebView)findViewById(R.id.webView);
        urlText=(EditText) findViewById(R.id.editText);
        webView.loadUrl(initURL);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view,String url){
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if(url.startsWith("http")){
                    urlText.setText(url);
                }
            }
        });
        WebSettings webSettings=webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if(newProgress==100){
                    closeDialog();
                }else{
                    openDialog(newProgress);
                }
            }

            private void closeDialog(){
                if(dialog!=null&&dialog.isShowing()){
                    dialog.dismiss();
                    dialog=null;
                }
            }

            private void openDialog(int newProgress){
                if(dialog==null){
                    dialog=new ProgressDialog(MainActivity.this);
                    dialog.setTitle("正在加载");
                    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    dialog.setProgress(newProgress);
                    dialog.show();
                }else{
                    dialog.setProgress(newProgress);
                }
            }
        });

        urlText.setOnEditorActionListener(new TextView.OnEditorActionListener(){
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //当actionId == XX_SEND 或者 XX_DONE时都触发
                //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
                //注意,这是一定要判断event != null。因为在某些输入法上会返回null。
                if (actionId == EditorInfo.IME_ACTION_SEND
                        || actionId == EditorInfo.IME_ACTION_DONE
                        || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {
                    String text=urlText.getText().toString();
                    if(!text.startsWith("http://")){
                        text="http://"+text;
                    }
                    webView.loadUrl(text);
                }
                return false;
            }
        });
    }

    @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);
    }
}
作者:张雪飞
出处:https://zhangxuefei.site/p/1947
版权说明:欢迎转载,但必须注明出处,并在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

发表评论

电子邮件地址不会被公开。 必填项已用*标注