android 簡易顯示web瀏覽,嵌入本身的應用中java
使用android 自帶的webView便可實現android
package com.example.tanhuohui; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.view.View; import android.view.View.OnClickListener; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ImageView; import android.widget.LinearLayout; /** * 請關注衛浴家居,歐風衛浴 * IP地址: * 手機店鋪地址:http://shop102621944.m.taobao.com * */ public class WebBrowser extends Activity implements OnClickListener{ WebView mWebView; ImageView back; ImageView forward; ImageView refresh; LinearLayout goback,goforward,refreshBTn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.web_browser); setControl(); setWebStyle(); } private void setControl() { mWebView=(WebView)findViewById(R.id.webshow); //first=(Button)findViewById(R.id.button1); back=(ImageView)findViewById(R.id.button2); forward=(ImageView)findViewById(R.id.button3); refresh=(ImageView)findViewById(R.id.button4); goback=(LinearLayout) findViewById(R.id.goBackBtn); goforward=(LinearLayout) findViewById(R.id.goForwardBtn); refreshBTn=(LinearLayout) findViewById(R.id.refreshBtn); goback.setOnClickListener(this); goforward.setOnClickListener(this); refreshBTn.setOnClickListener(this); } @SuppressLint("SetJavaScriptEnabled") private void setWebStyle() { mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setSupportZoom(true); mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); mWebView.requestFocus(); mWebView.loadUrl("http://shop102621944.m.taobao.com"); mWebView.setWebViewClient(new MyWebViewClient()); // 設置setWebChromeClient對象 mWebView.setWebChromeClient(chromeClient); } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v == goback) { if (mWebView.canGoBack()){ mWebView.goBack(); } } else if (v == goforward) { if (mWebView.canGoForward()){ mWebView.goForward(); } }else if(v==refreshBTn){ mWebView.reload(); } } class MyWebViewClient extends WebViewClient{ @Override public boolean shouldOverrideUrlLoading(WebView view,String url_){ view.loadUrl(url_); goback.setEnabled(mWebView.canGoBack()); if(mWebView.canGoBack()){ back.setImageResource(R.drawable.mini_webview_back); }else{ back.setImageResource(R.drawable.mini_webview_back_disable); } goforward.setEnabled(mWebView.canGoForward()); if(mWebView.canGoForward()){ forward.setImageResource(R.drawable.mini_webview_forward); }else{ forward.setImageResource(R.drawable.mini_webview_back_disable); } return true; } @Override public void onPageFinished(WebView view, String url) { //設置程序的標題爲網頁的標題 } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { goback.setEnabled(mWebView.canGoBack()); if(mWebView.canGoBack()){ back.setImageResource(R.drawable.mini_webview_back); }else{ back.setImageResource(R.drawable.mini_webview_back_disable); } goforward.setEnabled(mWebView.canGoForward()); if(mWebView.canGoForward()){ forward.setImageResource(R.drawable.mini_webview_forward); }else{ forward.setImageResource(R.drawable.mini_webview_forward_disable); } } } WebChromeClient chromeClient=new WebChromeClient(){ public void onReceivedTitle(WebView view, String title) { //顯示網頁標題 //titleText.setText(title); }; public void onProgressChanged(WebView view, int newProgress) { if(newProgress==100){ refreshBTn.setEnabled(true); refresh.setImageResource(R.drawable.mini_webview_refresh); }else{ refresh.setImageResource(R.drawable.mini_webview_refresh_click); refreshBTn.setEnabled(false); } }; }; }