import com.droidyue.demo.webviewprogressbar.R;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar bar = (ProgressBar)findViewById(R.id.myProgressBar);
final WebView webView = (WebView)findViewById(R.id.myWebView);
//允许JavaScript执行
webSettings.setJavaScriptEnabled(true);
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
bar.setVisibility(View.INVISIBLE);
} else {
if (View.INVISIBLE == bar.getVisibility()) {
bar.setVisibility(View.VISIBLE);
}
bar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
});
findViewById(R.id.myButton).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
webView.reload();
}
});
final String url = "http://jb51.net";
webView.loadUrl(url);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
////////////////////////////////////////////////////////////////
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/myProgressBar"
android:indeterminateOnly="false" //不显示后台进度
android:layout_below="@id/myButton"
android:layout_width="match_parent"
android:layout_height="5dp"
/>
<WebView
android:id="@+id/myWebView"
android:layout_below="@id/myProgressBar"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
//允许JavaScript执行
webSettings.setJavaScriptEnabled(true);
// 添加一个对象, 让javascript可以访问该对象的方法,
myWebView.addJavascriptInterface(new WebAppInterface(this),
“myInterfaceName”);
//java中调用javascript中的方法
myWebView.loadUrl(“javascript:myFunction()”);
有任何疑问或技术合作都可联系我
微信:yanfahezuo 【推荐】
QQ:596957738