本节引言:
1.页面错误,加载自定义网页:运行效果图: 关键代码: wView.setWebViewClient(new WebViewClient() { //设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中 @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); wView.loadUrl("file:///android_asset/error.html"); } }); 2.页面错误,显示相应的View运行效果图: 实现代码: public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private WebView wView; private ImageView img_error_back; private Button btn_refresh; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wView = (WebView) findViewById(R.id.wView); img_error_back = (ImageView) findViewById(R.id.img_error_back); btn_refresh = (Button) findViewById(R.id.btn_refresh); wView.loadUrl("http://www.baidu.com"); wView.setWebViewClient(new WebViewClient() { //设置在webView点击打开的新网页在当前界面显示,而不跳转到新的浏览器中 @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); wView.setVisibility(View.GONE); img_error_back.setVisibility(View.VISIBLE); } }); btn_refresh.setOnClickListener(this); } @Override public void onClick(View v) { wView.loadUrl("http://www.baidu.com"); img_error_back.setVisibility(View.GONE); wView.setVisibility(View.VISIBLE); } } 3.示例代码下载:WebViewDemo8.zip:下载 WebViewDemo8.zip 本节小结:
|
|
来自: 小飞苑 > 《android基础》