AlertDialog替代ProgressDialog方案

在Android API level 26及以后版本中,ProgressDialog被弃用,使用以下方法可以替代ProgressDialog。

首先,使用AlertDialog新建一个对象作为容器,在这里我们首先设置对话标题,然后取消然后关闭返回键退出对话框(注意:这里我们并不设置对话框的内容,以及返回键和确定键)。

AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("This is Dialog");
dialog.setCancelable(false);

然后,我们在这个对象里添加一个View对象,这里我们首先在layout文件夹中新建一个布局,在这个文件夹里我们使用ProgressBar和TextView来进行模拟:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="30dp" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_marginRight="40dp"
        android:textSize="20dp"
        android:textColor="#999"
        android:text="Loading..."/>
</LinearLayout>


​

效果如图:

接下来调用对象的setView()方法来放置该布局,

dialog.setView(R.layout.*);

接下来显示对话框:

dialog.show();

由于该类不能自动关闭对话框,所以在这我们使用其父类Dialog来实现对话框的关闭,首先创建一个对象并将dialog.show()的返回值赋值给该对象:

Dialog dialog1 = dialog.show();

最后在我们想要关闭该对话空时调用其方法dialog1.dismiss()即可。

效果如下:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值