我发现ProgressDialog现在已经被弃用了。除了ProgressBar,还有什么替代工具可以代替它。 我使用的是android studio 2.3.3版。
ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.show();
我发现ProgressDialog现在已经被弃用了。除了ProgressBar,还有什么替代工具可以代替它。 我使用的是android studio 2.3.3版。
ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.show();
当前回答
我使用来自https://github.com/Q115/DelayedProgressDialog的DelayedProgressDialog,它与ProgressDialog相同,在必要时具有延迟的额外好处。
使用它类似于Android O之前的ProgressDialog:
DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");
其他回答
是的,ProgressDialog已弃用,但Dialog没有。
您可以将自己的XML文件(包含进度条和加载文本)扩展到对话框对象中,然后使用show()和dismiss()函数显示或隐藏它。 下面是一个例子(Kotlin):
ProgressDialog类:
class ProgressDialog {
companion object {
fun progressDialog(context: Context): Dialog{
val dialog = Dialog(context)
val inflate = LayoutInflater.from(context).inflate(R.layout.progress_dialog, null)
dialog.setContentView(inflate)
dialog.setCancelable(false)
dialog.window!!.setBackgroundDrawable(
ColorDrawable(Color.TRANSPARENT))
return dialog
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:background="#fff"
android:padding="13dp"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="100dp"
android:layout_margin="7dp"
android:layout_height="100dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="7dp"
android:layout_toEndOf="@+id/progressBar"
android:text="Loading..." />
</RelativeLayout>
在你的代码中: 只需执行var dialog = progressdialog。progressdialog (context)
显示方法:dialog.show()
隐藏方法:dialog.dismiss()
ProgressBar非常简单易用, 我打算制作一个同样简单的进度对话框。 第一步是你可以为你想要显示的对话框创建XML布局,假设我们命名这个布局
layout_loading_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="20dp">
<ProgressBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:text="Please wait! This may take a moment." />
</LinearLayout>
下一步是创建AlertDialog,它将用ProgressBar显示这个布局
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(false); // if you want user to wait for some process to finish,
builder.setView(R.layout.layout_loading_dialog);
AlertDialog dialog = builder.create();
现在剩下的就是在点击事件中显示和隐藏这个对话框 像这样
dialog.show(); // to show this dialog
dialog.dismiss(); // to hide this dialog
这就是它,它应该工作,因为你可以看到它是相当简单和容易实现的ProgressBar而不是ProgressDialog。 现在你可以在Handler或ASyncTask中显示/取消这个对话框,这取决于你的需要
您可以简单地为progressbar设计一个xml接口,并将其作为一个视图传递给AlertDialog,然后在任何您需要的时候显示或关闭该对话框。
progress.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="13dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/loader"
android:layout_marginEnd="5dp"
android:layout_width="45dp"
android:layout_height="45dp" />
<TextView
android:layout_width="wrap_content"
android:text="Loading..."
android:textAppearance="?android:textAppearanceSmall"
android:layout_gravity="center_vertical"
android:id="@+id/loading_msg"
android:layout_toEndOf="@+id/loader"
android:layout_height="wrap_content" />
</LinearLayout>
显示进度对话框的代码代码。只需复制这段代码并粘贴它的片段。
Dialog dialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(R.layout.progress);
// This should be called once in your Fragment's onViewCreated() or in Activity onCreate() method to avoid dialog duplicates.
dialog = builder.create();
}
// This method is used to control the progress dialog.
private void setDialog(boolean show){
if (show)dialog.show();
else dialog.dismiss();
}
然后只要在你想要显示progressdialog时调用这个方法,并将true作为参数来显示它,或将false作为参数来取消对话框。
ProgressDialog在API级别26中已弃用。
“已弃用”指的是正在被更新的函数或元素替换的函数或元素。
ProgressDialog是一个模态对话框,它阻止用户与之交互 而不是使用这个类,你应该使用一个进度 比如ProgressBar,它可以嵌入到你的应用程序的 UI。
优势
我个人认为ProgressBar比这两者更有优势。ProgressBar是一个指示操作进度的用户界面元素。以非中断的方式向用户显示进度条。在应用程序的用户界面中显示进度条。
Kotlin和androidx DialogFragment的另一个解决方案
通过使用DialogFragment,你不必每次都在需要显示对话框的每个屏幕上调整窗口的大小,我认为这是一个更干净的解决方案
创建对话框类
class BaseProgressDialog : DialogFragment() {
companion object {
const val DIALOG_TAG = "BaseProgressDialogFragment"
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it, R.style.MainAlertDialog)
val inflater = requireActivity().layoutInflater
val dialogView = inflater.inflate(R.layout.progress_dialog, null)
builder.setView(dialogView)
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
dialog?.setCanceledOnTouchOutside(false)
dialog?.setCancelable(false)
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onResume() {
super.onResume()
val size = resources.getDimensionPixelSize(R.dimen.size_80dp)
dialog?.window?.setLayout(size, size)
}
}
在styles.xml中创建样式
<style name="MainAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@drawable/bg_alert_dialog</item>
</style>
为对话框bg_alert_dialog.xml创建背景
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/colorSecondary"/>
<corners android:radius="@dimen/radius_10dp" />
</shape>
为对话框progress_dialog.xml创建布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_alert_dialog"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/padding_16dp">
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="@dimen/size_40dp"
android:layout_height="@dimen/size_40dp"
android:indeterminate="true"
app:indicatorColor="?attr/colorPrimary" />
</LinearLayout>
调用片段中的对话框
private var progressDialog = BaseProgressDialog()
activity?.let {
progressDialog.show(it.supportFragmentManager, BaseProgressDialog.DIALOG_TAG)
}
这是它看起来的样子: