search

Home  >  Q&A  >  body text

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

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();
    }
大家讲道理大家讲道理2892 days ago598

reply all(3)I'll reply

  • PHPz

    PHPz2017-04-17 17:59:17

    Don’t call UI in child thread

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

    Try this

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:59:17

    I want to achieve such an effect, when I request the api, make a loading effect. Now my loading box doesn't show up.

    This is my request api class (NetworkTask), which inherits AsyncTask; I encapsulate all requests here.

    This is my code for calling NetworkTask. The values ​​passed by the constructor are the api name to be called and the current activity (Activity).

    onPostExecute and onPreExecute are both called, and the dialog also has a value;

    I heard that child threads cannot call ui? Is it because of this problem? So I can't call the dialog?

    The following is the layout of my dialog box and the java code of the dialog box

    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;
        }
    
    }

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 17:59:17

    There is an overridden method onProgressUpdate, and the dialog box is written in it

    reply
    0
  • Cancelreply