前言:
而今各位老铁们对“ioswebview加载html”可能比较珍视,姐妹们都需要分析一些“ioswebview加载html”的相关内容。那么小编在网摘上网罗了一些有关“ioswebview加载html””的相关文章,希望咱们能喜欢,看官们一起来学习一下吧!WebView 加载判断 和 加载动画的简单实现
WebView 在加载网页时候,在网络不好的情况下,如果不做处理会出现一段时间的等待空白,用户体验差。所以有必要加上一个简单的加载进度提示。本编简单的运用了ProgressDialog 这个控件来实现。
下面是简单的代码片段
<span style="color:#ff6666;"><</span>?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:background="@color/colorPrimaryDark" android:popupTheme="@style/AppTheme.PopupOverlay" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" > <ImageView android:id="@+id/faq_activity_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/back"/> <TextView android:id="@+id/faq_activity_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textColor="@color/white" android:textSize="22sp" android:text="常见问题" /> </android.support.v7.widget.Toolbar> <WebView android:id="@+id/faq_webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>下面贴上部分代码public class FAQActivity extends BaseActivity { private final String TAG = FAQActivity.class.getName(); @BindView(R.id.activity_fap_back) ImageView activityFapBack; @BindView(R.id.activity_faq_webview) WebView webView; private String FAQ_BASE_URL = ""; /*** * * @param savedInstanceState */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_faq); ButterKnife.bind(this); init(); } private void init() { webView.loadUrl("你的请求地址"); WebSettings mWebSettings = webView.getSettings(); mWebSettings.setSupportZoom(true); mWebSettings.setLoadWithOverviewMode(true); mWebSettings.setUseWideViewPort(true); mWebSettings.setDefaultTextEncodingName("GBK");//设置解码格式 mWebSettings.setLoadsImagesAutomatically(true); mWebSettings.setJavaScriptEnabled(true);//支持js 特效 //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为,使网页用WebView打开 webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url); return true; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); showProgress("页面加载中");//开始加载动画 } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); removeProgress();//当加载结束时移除动画 } }); } /** * 点击事件 */ @OnClick(R.id.activity_fap_back) public void onClick() { Log.d(TAG, "onClick: "); jumpToActivity(FAQActivity.this,MenuActivity.class); } /** * 返回键的监听事件 * * @param keyCode * @param event * @return */ public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { jumpToActivity(FAQActivity.this,MenuActivity.class); } return false; } }
需要注意的地方:ProgressDIalog 控件为了不写重复代码,我进行了简单的封装。下面是我简单封装的BaseActivity代码
public class BaseActivity extends AppCompatActivity { private final String TAG = BaseActivity.class.getName(); private Toast toast = null; private ProgressDialog progressDialog; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); } /** * @param context 上下文对象 * @param start 其实activity * @param target 目标activity */ public void jumpToActivity(Context context, Class start, Class target) { Intent intent = new Intent(context, target); //------用户返回上一级activity if (target == CarFindingActivity.class) { intent.putExtra("activity", start); } context.startActivity(intent); finish(); } public void jumpToActivity(Context context, Class target) { Intent intent = new Intent(context, target); //------用户返回上一级activity context.startActivity(intent); finish(); } /** * @param context * @param text */ public void showToastLong(Context context, String text) { if (toast == null) { toast = Toast.makeText(context, text, Toast.LENGTH_LONG); } else { toast.setText(text); } toast.show(); } public void showToastLong(String text) { if (toast == null) { toast = Toast.makeText(BaseActivity.this, text, Toast.LENGTH_LONG); } else { toast.setText(text); } toast.show(); } /** * @param context * @param text */ public void showToastShort(Context context, String text) { if (toast == null) { toast = Toast.makeText(context, text, Toast.LENGTH_SHORT); } else { toast.setText(text); } toast.show(); } public void showToastShort(String text) { if (toast == null) { toast = Toast.makeText(BaseActivity.this, text, Toast.LENGTH_SHORT); } else { toast.setText(text); } toast.show(); } //-----显示ProgressDialog public void showProgress(String message) { if (progressDialog == null) { progressDialog = new ProgressDialog(BaseActivity.this, ProgressDialog.STYLE_SPINNER); progressDialog.setCancelable(false);//设置点击不消失 } if (progressDialog.isShowing()) { progressDialog.setMessage(message); } else { progressDialog.setMessage(message); progressDialog.show(); } } //------取消ProgressDialog public void removeProgress(){ if (progressDialog==null){ return; } if (progressDialog.isShowing()){ progressDialog.dismiss(); } } /** * */ @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart: "); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume: "); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause: "); } @Override protected void onStop() { removeProgress();//让progressdialog 消失 super.onStop(); Log.d(TAG, "onStop: "); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart: "); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy: "); } }
然后继承BaseActivtiy 直接调用里面
showProgress(String message)
显示ProgressDialogremoveProgress()
移除ProgressDialog以上就这么多,如有错误或者不严谨的地方恳请指正,谢谢
标签: #ioswebview加载html