通过WebView实现简单的浏览器
对于WebView组件、虽然在做非浏览器app的时候用到的不多、但还是对这个组件的用法有个基本的了解、下面通过实现一个简单的浏览器的基本功能来熟悉下WebView组件、和它的一些基本使用方法、
布局文件:activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ScrollView android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/layout" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/margin_small"> <EditText android:id="@+id/et_url" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="textUri" android:singleLine="true" android:layout_margin="@dimen/margin_small"/> <Button android:id="@+id/btn_visit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="@dimen/margin_small" android:text="@string/visit" android:textSize="@dimen/text_medium" /> </LinearLayout> <WebView android:id="@+id/web" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> </ScrollView> <LinearLayout android:id="@+id/ll_btn" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> <Button android:id="@+id/btn_home" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/home"/> <Button android:id="@+id/btn_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/left"/> <Button android:id="@+id/btn_right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/right"/> <Button android:id="@+id/btn_exit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/exit"/> </LinearLayout> </LinearLayout>
简单浏览器实现代码:
public class MainActivity extends Activity implements OnClickListener{ private EditText et_url; private Button btn_visit; private WebView web; private Button btn_home,btn_left,btn_right,btn_exit; private boolean isExit; private String homeStr = "http://www.baidu.com"; //------------------------------------------- /** * 基本数据初始化 */ private void init(){ isExit = false; et_url = (EditText)findViewById(R.id.et_url); et_url.setText(homeStr); btn_visit = (Button)findViewById(R.id.btn_visit); web = (WebView)findViewById(R.id.web); WebSettings ws = web.getSettings(); //是否允许脚本支持 ws.setJavaScriptEnabled(true); ws.setJavaScriptCanOpenWindowsAutomatically(true); ws.setSaveFormData(false); ws.setSavePassword(false); ws.setAppCacheEnabled(true); ws.setAppCacheMaxSize(10240); // ws.setCacheMode(WebSettings.LOAD_NO_CACHE); //是否允许缩放 // ws.setBuiltInZoomControls(true); web.setWebViewClient(wvc); web.setWebChromeClient(wcc); btn_home = (Button)findViewById(R.id.btn_home); btn_left = (Button)findViewById(R.id.btn_left); btn_right = (Button)findViewById(R.id.btn_right); btn_exit = (Button)findViewById(R.id.btn_exit); btn_visit.setOnClickListener(this); btn_home.setOnClickListener(this); btn_left.setOnClickListener(this); btn_right.setOnClickListener(this); btn_exit.setOnClickListener(this); web.setOnTouchListener(touchListener); conn(et_url.getText().toString()); } //------------------------------------------- /** * 触摸监听 */ OnTouchListener touchListener = new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { switch(v.getId()){ case R.id.web: web.requestFocus(); break; } return false; } }; //------------------------------------------- public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); init(); } //------------------------------------------- WebViewClient wvc = new WebViewClient(){ public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); et_url.setText(url); return true; }; }; //------------------------------------------- WebChromeClient wcc = new WebChromeClient(){ public void onRequestFocus(WebView view) { super.onRequestFocus(view); view.requestFocus(); } }; //------------------------------------------- /** * 访问url * @param urlStr */ private void conn(String urlStr){ String url = ""; if(urlStr.contains("http://")){ url = urlStr; }else{ url = "http://"+urlStr; } web.loadUrl(url); } //------------------------------------------- /** * 后退 */ private void goBack(){ if(web.canGoBack()){ web.goBack(); }else{ Toast.makeText(this, "已经是第一页",Toast.LENGTH_SHORT).show(); } } //------------------------------------------- /** * 前进 */ private void goForward(){ if(web.canGoForward()){ web.goForward(); }else{ Toast.makeText(this, "已经是最后一页",Toast.LENGTH_SHORT).show(); } } //------------------------------------------- /** * 退出 */ private void exit(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示") .setMessage("确定退出?") .setPositiveButton("确定", dialogListener) .setNegativeButton("取消", dialogListener) .create() .show(); } DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch (which) { case DialogInterface.BUTTON_POSITIVE: finish(); break; default: dialog.cancel(); break; } } }; //------------------------------------------- public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK||web.canGoBack()){ web.goBack(); if(!web.canGoBack()){ if(isExit){ return super.onKeyDown(keyCode, event); } isExit = true; Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show(); }else{ isExit = false; } } return true; } //------------------------------------------- public void onClick(View v) { switch (v.getId()) { case R.id.btn_visit: conn(et_url.getText().toString()); break; case R.id.btn_home: conn(homeStr); break; case R.id.btn_left: goBack(); break; case R.id.btn_right: goForward(); break; case R.id.btn_exit: exit(); break; } } }
需要用到的联网权限:
<uses-permission android:name="android.permission.INTERNET"/>
这样一个简单的浏览器就实现了、我们来看下效果、
效果图如下:
关于作者
Name: Jenly
Email: jenly1314@gmail.com
Email: jenly1314@vip.qq.com
Github: github.com/jenly1314