android - 请求网络的时候出现自定义的对话框
问题描述
public static Dialog createLoadingDialog(Context context){LayoutInflater inflater = LayoutInflater.from(context);View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载viewLinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局// main.xml中的ImageViewImageView spaceshipImage = (ImageView) v.findViewById(R.id.imgLoading);SceneAnimation s = new SceneAnimation(spaceshipImage,imglist,1);//Glide.with(context).load(R.drawable.et_loader).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(spaceshipImage);Dialog loadingDialog = new Dialog(context,R.style.loadingDialogStyle);// 创建自定义样式dialogloadingDialog.setCancelable(true);// 不可以用“返回键”取消loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));// 设置布局return loadingDialog; }
这是我对话框的代码;
@Override protected void onPostExecute(String s) {super.onPostExecute(s);dialog.dismiss(); }@Override protected void onPreExecute() {super.onPreExecute();dialog = LoadingDialog.createLoadingDialog(activity);dialog.show(); }
问题解答
回答1:不要在子线程中调用UI
activity.runOnUiThread(new Runnable() { @Override public void run() {dialog.show(); }});
这样试试
回答2:我想实现这样一个效果,在我请求api的时候,做出一个加载中的效果。 现在我的加载框显示不了。
这是我的请求api的类(NetworkTask),它继承了AsyncTask; 我把所有请求都封装在这里。
这是我调用 NetworkTask 的 代码 。 构造函数传的值是 需要调用的 api 名称 和 当前 活动 (Activity) 。
onPostExecute 和 onPreExecute 都 被调用了,dialog 也 有值;
我听说 子线程 不能 调用 ui ? 是因为这个问题吗?所以我不能够 调用 对话框 ?
以下 是我对话框的布局 和 对话框的 java 代码
public class LoadingDialog { private static int[] imglist = new int[]{ R.drawable.et_loading_00, R.drawable.et_loading_01, R.drawable.et_loading_02, R.drawable.et_loading_03, R.drawable.et_loading_04, R.drawable.et_loading_05, R.drawable.et_loading_06, R.drawable.et_loading_07, R.drawable.et_loading_08, R.drawable.et_loading_09, R.drawable.et_loading_10, R.drawable.et_loading_11, R.drawable.et_loading_12, R.drawable.et_loading_13, R.drawable.et_loading_14, R.drawable.et_loading_15 }; public static Dialog createLoadingDialog(Context context){LayoutInflater inflater = LayoutInflater.from(context);View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载viewLinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局// main.xml中的ImageViewImageView spaceshipImage = (ImageView) v.findViewById(R.id.imgLoading);SceneAnimation s = new SceneAnimation(spaceshipImage,imglist,1);//Glide.with(context).load(R.drawable.et_loader).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(spaceshipImage);Dialog loadingDialog = new Dialog(context,R.style.loadingDialogStyle);// 创建自定义样式dialogloadingDialog.setCancelable(true);// 不可以用“返回键”取消loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));// 设置布局return loadingDialog; }}回答3:
有个重写的方法 onProgressUpdate,把对话框写在里面
相关文章:
1. python - (初学者)代码运行不起来,求指导,谢谢!2. 为什么python中实例检查推荐使用isinstance而不是type?3. mysql里的大表用mycat做水平拆分,是不是要先手动分好,再配置mycat4. window下mysql中文乱码怎么解决??5. sass - gem install compass 使用淘宝 Ruby 安装失败,出现 4046. html5 - H5 SSE的本质是什么?7. javascript - h5上的手机号默认没有识别8. python - 获取到的数据生成新的mysql表9. python的文件读写问题?10. javascript - js 对中文进行MD5加密和python结果不一样。
