android - 请求网络的时候出现自定义的对话框
大家讲道理
大家讲道理 2017-04-17 17:57:17
0
3
555
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();
    }
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(3)
PHPzhong

Don’t call UI in child thread

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

Try this

大家讲道理

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

}
巴扎黑

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template