我发现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();
当前回答
在进度对话框中,用户不能做任何工作。所有后台进程在进度对话框中停止。因此,建议使用进度条代替进度对话框。
其他回答
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中显示/取消这个对话框,这取决于你的需要
创建一个带有进度条和其他视图的xml文件,并使用此xml文件或Alertdialog派生类扩展派生Dialog类视图。在其他解决方案中已详细提供。 但是如果你想要一个解或者存在的库。我找到了这两个。
https://github.com/Livin21/MissMe https://github.com/skydoves/ProgressView
这些都是完美的,而且是根据需要来做的。
下面是@Han建议的ProgressDialog类的kotlin版本
class ProgressDialog(context: Context) : Dialog(context) {
init {
val view = View.inflate(context, R.layout.progress_dialog, null)
setContentView(view)
setCancelable(false)
window?.setBackgroundDrawable(
ColorDrawable(Color.TRANSPARENT)
)
}
}
您不需要导入任何自定义库。
我更喜欢使用现代的AlertDialog,所以这是Kishan Donga在本页发布的伟大答案的Kotlin版本。
芬兰湾的科特林代码:
fun setProgressDialog(context:Context, message:String):AlertDialog {
val llPadding = 30
val ll = LinearLayout(context)
ll.orientation = LinearLayout.HORIZONTAL
ll.setPadding(llPadding, llPadding, llPadding, llPadding)
ll.gravity = Gravity.CENTER
var llParam = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
llParam.gravity = Gravity.CENTER
ll.layoutParams = llParam
val progressBar = ProgressBar(context)
progressBar.isIndeterminate = true
progressBar.setPadding(0, 0, llPadding, 0)
progressBar.layoutParams = llParam
llParam = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
llParam.gravity = Gravity.CENTER
val tvText = TextView(context)
tvText.text = message
tvText.setTextColor(Color.parseColor("#000000"))
tvText.textSize = 20.toFloat()
tvText.layoutParams = llParam
ll.addView(progressBar)
ll.addView(tvText)
val builder = AlertDialog.Builder(context)
builder.setCancelable(true)
builder.setView(ll)
val dialog = builder.create()
val window = dialog.window
if (window != null) {
val layoutParams = WindowManager.LayoutParams()
layoutParams.copyFrom(dialog.window?.attributes)
layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT
layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT
dialog.window?.attributes = layoutParams
}
return dialog
}
用法:
val dialog = setProgressDialog(this, "Loading..")
dialog.show()
输出:
我使用来自https://github.com/Q115/DelayedProgressDialog的DelayedProgressDialog,它与ProgressDialog相同,在必要时具有延迟的额外好处。
使用它类似于Android O之前的ProgressDialog:
DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");