package com.example.mr18_xzvon.myapplication;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private String url="http://www.imooc.com/course/list";
private WebView webView;
private ProgressDialog dialog = new ProgressDialog(MainActivity.this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
//Uri uri=Uri.parse(url);
//Intent intent=new Intent(Intent.ACTION_VIEW,uri);
//startActivity(intent);
init();
}
private void init() {
webView= (WebView) findViewById(R.id.webView);
webView.loadUrl("http://www.imooc.com/course/list");
//覆盖WebView默认通过第三方或者是浏览器打开网页的行为,使得网页可以在WebView中打开
webView.setWebViewClient(new WebViewClient(){
public boolean shouldOerridUrloading(WebView view,String url){
//返回值是true的时候控制网页在WebView中去打开,如果为false调用系统浏览器或者第三方浏览器打开
view.loadUrl(url);
return true;
}
//WebViewClient帮助WebView去处理一些页面控制和请求通知
});
//启用支持Javascript
WebSettings settings=webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient(){
public void noProgressChanged(WebView view,int newProgress){
//newProgress 1-100之间的整数
if(newProgress==100){
//网页加载完毕,关闭ProgressDialog
closeDialog();
}else{
//网页正在加载,打开ProgressDialog
openDialog(newProgress);
}
}
private void openDialog(int newProgress) {
if (dialog==null){
dialog=new ProgressDialog(MainActivity.this);
dialog.setTitle("正在加载");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(newProgress);
dialog.show();
}
else{
dialog.setProgress(newProgress);
}
}
private void closeDialog() {
if(dialog!=null&&dialog.isShowing()){
dialog.dismiss();
dialog=null;
}
}
});
}
//改写物理按键--返回的逻辑
public boolean onKeyDown(int keyCode,KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_BACK){
if(webView.canGoBack()){
Toast.makeText(this,webView.getUrl(),Toast.LENGTH_SHORT).show();
webView.goBack();//返回上一页面
return true;
}
else
{
System.exit(0);
}
}
return super.onKeyDown(keyCode,event);
}
}
目前暂无任何回答
- 0 回答
- 0 关注
- 1410 浏览
添加回答
举报
0/150
提交
取消