您的当前位置:首页正文

android 简易显示web浏览,嵌入自己的应用中

2024-11-07 来源:个人技术集锦

android 简易显示web浏览,嵌入自己的应用中

使用android 自带的webView即可实现

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);
			}
		};
		
	};
}

  

转载于:https://my.oschina.net/u/579305/blog/760714

显示全文