dialog弹框 动态提示数据加载中...

老规矩先看效果图

我是给个点击事件,然后弹出数据加载,3秒后消失

  • 主代码

 

public class MyDialog extends AppCompatActivity {
    CustomDialog customDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_dialog);
    }
    //点击按钮事件的的处理
    public void onclick(View view) {
        customDialog = new CustomDialog(this, R.style.CustomDialog,"数据加载中...");
        if (!customDialog.isShowing()){//弹框没有显示
            customDialog.show();//显示
        }
        //延时任务,我做的是3秒后消失,你可以根据你的情况,在获取到数据后,直接调用dismissDialog(); 或者任意时刻直接调用dismissDialog();停止加载弹框...
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);//3秒后弹框消失
                    //你可以在获取到数据后,调用dismissDialog(); 或者任意时刻调用dismissDialog();停止加载弹框...
                    dismissDialog();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void dismissDialog() {
       if (customDialog != null && customDialog.isShowing()) {
           customDialog.dismiss();
           customDialog = null;
       }
    }

}
  • activity_my_dialog.xml 把这个也贴出来吧
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyDialog">

    <Button
        android:onClick="onclick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="点击"
        />

</LinearLayout>
  • CustomDialog代码
public class CustomDialog extends ProgressDialog {
    String xx="";
    public TextView tv;

    public CustomDialog(Context context, String msg) {
        super(context);
        xx=msg;
    }

    public CustomDialog(Context context, int theme, String msg) {
        super(context, theme);
        xx=msg;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        //设置不可取消,点击其他区域不能取消,
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.custom_progress_dialog);
        tv= (TextView) this.findViewById(R.id.tvforprogress);
        tv.setText(xx);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
                        //布局位于状态栏下方
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        //全屏
                        View.SYSTEM_UI_FLAG_FULLSCREEN |
                        //隐藏导航栏
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
                if (Build.VERSION.SDK_INT >= 19) {
                    uiOptions |= 0x00001000;
                } else {
                    uiOptions |= View.SYSTEM_UI_FLAG_LOW_PROFILE;
                }
                getWindow().getDecorView().setSystemUiVisibility(uiOptions);
            }
        });
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
    }

    @Override
    public void show() {
        super.show();
    }
}
  • custom_progress_dialog布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="200dp"
    android:layout_height="60dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:orientation="horizontal" >

    <ProgressBar
        android:id="@+id/ios_progressbar"
        style="@style/DefineprogressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:layout_marginLeft="16dp"
        android:layout_marginBottom="0dp" />

    <TextView
        android:id="@+id/tvforprogress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="0dp"
        android:textColor="@android:color/white"
        android:text="加载中..."
        android:textSize="14dp" />
</LinearLayout>
  • DefineprogressBarStyleSmall values里面的styles.xml
   <!-- 加载进度 -->
    <style name="DefineprogressBarStyleSmall" parent="android:style/Widget.ProgressBar">
        <item name="android:indeterminateDrawable">@drawable/progress_medium</item>
        <item name="android:minWidth">30dp</item>
        <item name="android:maxWidth">30dp</item>
        <item name="android:minHeight">30dp</item>
        <item name="android:maxHeight">30dp</item>
    </style>

  • progress_medium 是drawable里面新建的的一个xml文件
<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@mipmap/home_loading_50px"
    android:pivotX="50%"
    android:pivotY="50%">
</animated-rotate>

home_loading_50px.png 图片

 



————————————————
版权声明:本文为CSDN博主「AaVictory.」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/afufufufu/article/details/123713005

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值