android - 请求网络的时候出现自定义的对话框

 Mango-家族 发布于 2022-10-28 12:15
public static Dialog createLoadingDialog(Context context){
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // main.xml中的ImageView
        ImageView 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);// 创建自定义样式dialog

        loadingDialog.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();
    }
3 个回答
  • 有个重写的方法 onProgressUpdate,把对话框写在里面

    2022-10-29 18:06 回答
  • 我想实现这样一个效果,在我请求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);// 得到加载view
            LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
            // main.xml中的ImageView
            ImageView 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);// 创建自定义样式dialog
    
            loadingDialog.setCancelable(true);// 不可以用“返回键”取消
            loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
            return loadingDialog;
        }
    
    }
    2022-10-29 18:07 回答
  • 不要在子线程中调用UI

    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            dialog.show();
        }
    });

    这样试试

    2022-10-29 18:10 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有