自定义控件之带进度条的webview

public class ProgressWebView extends WebView {

private ProgressBar progressbar;

public ProgressWebView(Context context, AttributeSet attrs) {

super(context, attrs);

progressbar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);

progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 3, 0, 0));

addView(progressbar);

setWebChromeClient(new WebChromeClient());

setWebViewClient(new WebViewClient(){

public boolean shouldOverrideUrlLoading(WebView view, String url){ //  重写此方法表明点击网页里面的链接还是在当前的webview里跳转,不跳到浏览器那边

view.loadUrl(url);

return true;

}

});

}

public class WebChromeClient extends android.webkit.WebChromeClient {

@Override

public void onProgressChanged(WebView view, int newProgress) {

if (newProgress == 100) {

progressbar.setVisibility(GONE);

} else {

if (progressbar.getVisibility() == GONE)

progressbar.setVisibility(VISIBLE);

progressbar.setProgress(newProgress);

}

super.onProgressChanged(view, newProgress);

}

}

@Override

protected void onScrollChanged(int l, int t, int oldl, int oldt) {

LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();

lp.x = l;

lp.y = t;

progressbar.setLayoutParams(lp);

super.onScrollChanged(l, t, oldl, oldt);

}

}


public class WebActivity extends Activity implements OnClickListener{


private ProgressWebView web;

private RelativeLayout bar;

private TextView txt;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity2_web);

//顶部栏

bar=(RelativeLayout) findViewById(R.id.a2_w_bar);

bar.setOnClickListener(this);

txt=(TextView) findViewById(R.id.a2_w_title);

//webview

web=(ProgressWebView) findViewById(R.id.web_window);

WebSettings webSettings = web.getSettings();

//设置WebView属性,能够执行Javascript脚本

webSettings.setJavaScriptEnabled(true);

//设置可以访问文件

webSettings.setAllowFileAccess(true);

//设置支持缩放

webSettings.setBuiltInZoomControls(true);

//自适应屏幕

webSettings.setUseWideViewPort(true);

webSettings.setLoadWithOverviewMode(true);

webSettings.setBuiltInZoomControls(false);

web.setDownloadListener(new DownloadListener() {

@Override

public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {

if (url != null && url.startsWith("http://"))

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

}

});

//接收url

if("-1".equals(getIntent().getStringExtra("flag"))){

String url=getIntent().getStringExtra("url").replaceAll("amp;","");

String title=getIntent().getStringExtra("title");

txt.setText(title);

web.loadUrl(url);

}else if("0".equals(getIntent().getStringExtra("flag"))){

String url=getIntent().getStringExtra("url").replaceAll("amp;","");

String postData =getIntent().getStringExtra("parms");

txt.setText("我的体检表");

web.setWebViewClient(new WebViewClient(){

@Override

public void onLoadResource(WebView view, String url) {

super.onLoadResource(view, url);

view.loadUrl("javascript:function setHide(){$('.am-breadcrumb').hide();$('header').hide();$('footer').hide();}setHide();");

}

});

web.postUrl(url,EncodingUtils.getBytes(postData, "BASE64"));

}else if("1".equals(getIntent().getStringExtra("flag"))){

txt.setText(getIntent().getStringExtra("title"));

String url=getIntent().getStringExtra("url").replaceAll("amp;","");

String getdata ="unique="+SharedPreferencesUtil.getInstance().getUserName(WebActivity.this)+"&type=1";

web.loadUrl(url+"?"+getdata);

}

}

@Override

// 设置回退

// 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法

public boolean onKeyDown(int keyCode, KeyEvent event) {

if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {

web.goBack(); // goBack()表示返回WebView的上一页面

return true;

}

return super.onKeyDown(keyCode,event);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.a2_w_bar:

finish();

break;

// case R.id.a2_w_bar:

// break;

}

}

}

你可能感兴趣的:(自定义控件之带进度条的webview)