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

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

répondre à tous(3)
PHPzhong

N'appelez pas l'interface utilisateur dans les fils de discussion enfants

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

Essayez comme ça

大家讲道理

Je veux obtenir un tel effet, lorsque je demande à l'API, faire un effet de chargement. Maintenant, ma boîte de chargement n'apparaît pas.

Il s'agit de ma classe API de requête (NetworkTask), qui hérite d'AsyncTask ; j'encapsule toutes les requêtes ici.

Voici mon code pour appeler NetworkTask. Les valeurs transmises par le constructeur sont le nom de l'API à appeler et l'activité en cours (Activity).

onPostExecute et onPreExecute sont tous deux appelés, et la boîte de dialogue a également une valeur

;

J'ai entendu dire que les fils de discussion enfants ne peuvent pas appeler l'interface utilisateur ? Est-ce à cause de ce problème ? Donc je ne peux pas appeler la boîte de dialogue ?

Ce qui suit est la disposition de ma boîte de dialogue et le code java de la boîte de dialogue

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

}
巴扎黑

Il existe une méthode remplacée onProgressUpdate et la boîte de dialogue y est écrite

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal