一、首先创建爱你webActivity活动,并且创建web_layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_view_auto"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
Activity页面的重点:
一、创建init()函数
1.创建ProgressDialog控件
private ProgressDialog progressDialog;
//开启progressDialog
if(progressDialog==null){
progressDialog=new ProgressDialog(this);
}
progressDialog.show();
2.获取webview控件实例,设置webview的各个属性
webView=(WebView)findViewById(R.id.web_view_auto);
webView.getSettings().setJavaScriptEnabled(true);//支持javascript语言
//WebViewClient 主要处理关于页面跳转,页面请求等操作
webView.setWebViewClient(new WebViewClient(){
// @Override
// public boolean shouldOverrideUrlLoading(WebView view, String url) {
// load(url,webView);
// return super.shouldOverrideUrlLoading(view, url);
// }
});
//其实对于WebChromeClient,主要处理关于脚本的执行,或progress 等操作
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(newProgress==100){
// progressDialog.dismiss();
handler.sendEmptyMessage(MSG);
}
}
});
}
3.创建一个异步处理handler,用来控制progressDialog的关闭
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==MSG){
progressDialog.dismiss();
}
}
};
4.load()加载URL,重点为这里采用了webview独有的线程机制,***********
public void load(final String url, final WebView webView){
if(url==null||url.equals(""))
return;
webView.post(new Runnable() {
@Override
public void run() {
webView.loadUrl(url);
}
});
}
}
5.在oncreate中调用init(),load()
6.在autoActivity中实现页面的跳转
View digView=View.inflate(this,R.layout.authorize_dialog,null);dialog=new Dialog(this,R.style.auth_dialog);
dialog.setContentView(digView);
dialog.show();
button=(Button)digView.findViewById(R.id.btn_authorization);***********************************重点
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent=new Intent(AuthActivity.this,webActivity.class);
startActivity(intent);
}
package app.coolweather.com.weibo;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* Created by Administrator on 2016/8/6.
*/
public class webActivity extends Activity {
private WebView webView;
private ProgressDialog progressDialog;
private static final int MSG=1;
private String url="http://www.baidu.com";
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==MSG){
progressDialog.dismiss();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
init();
load(url,webView);
}
public void init(){
//开启progressDialog
if(progressDialog==null){
progressDialog=new ProgressDialog(this);
}
progressDialog.show();
webView=(WebView)findViewById(R.id.web_view_auto);
webView.getSettings().setJavaScriptEnabled(true);//支持javascript语言
//WebViewClient 主要处理关于页面跳转,页面请求等操作
webView.setWebViewClient(new WebViewClient(){
// @Override
// public boolean shouldOverrideUrlLoading(WebView view, String url) {
// load(url,webView);
// return super.shouldOverrideUrlLoading(view, url);
// }
});
//其实对于WebChromeClient,主要处理关于脚本的执行,或progress 等操作
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
if(newProgress==100){
// progressDialog.dismiss();
handler.sendEmptyMessage(MSG);
}
}
});
}
public void load(final String url, final WebView webView){
if(url==null||url.equals(""))
return;
webView.post(new Runnable() {
@Override
public void run() {
webView.loadUrl(url);
}
});
}
}