我发现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();
当前回答
也许这个指南可以帮助你。
通常我更喜欢用指示器制作自定义alertdialog。它解决了诸如自定义App视图等问题。
其他回答
下面是我的不确定进度对话框版本:
layout_loading_dialog.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:padding="20dp">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="4"
android:gravity="center"
android:textAlignment="textStart"
android:id="@+id/message"
tools:text="Please wait..." />
</LinearLayout>
IndeterminateProgressDialog.kt:
class IndeterminateProgressDialog(context: Context) : AlertDialog(context) {
private val messageTextView: TextView
init {
val view = LayoutInflater.from(context).inflate(R.layout.layout_loading_dialog, null)
messageTextView = view.findViewById(R.id.message)
setView(view)
}
override fun setMessage(message: CharSequence?) {
this.messageTextView.text = message.toString()
}
}
用法:
val dialog = IndeterminateProgressDialog(context)
dialog.setMessage("Please wait...")
dialog.setCanceledOnTouchOutside(false)
dialog.setCancelable(false)
dialog.show()
您可以简单地为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,那么使用 这个轻量级库。
Gradle设置
在module/build.gradle中添加以下依赖项:
compile 'com.lmntrx.android.library.livin.missme:missme:0.1.5'
如何使用它?
用法类似于原始的ProgressDialog
ProgressDialog progressDialog = new
progressDialog(YourActivity.this);
progressDialog.setMessage("Please wait");
progressDialog.setCancelable(false);
progressDialog.show();
progressDialog.dismiss();
注意:你必须覆盖活动的onBackPressed()
Java8实现:
@Override
public void onBackPressed() {
progressDialog.onBackPressed(
() -> {
super.onBackPressed();
return null;
}
);
}
芬兰湾的科特林实现:
override fun onBackPressed() {
progressDialog.onBackPressed { super.onBackPressed() }
}
参考示例应用程序的完整实现 完整的文档可以在这里找到
创建一个带有进度条和其他视图的xml文件,并使用此xml文件或Alertdialog派生类扩展派生Dialog类视图。在其他解决方案中已详细提供。 但是如果你想要一个解或者存在的库。我找到了这两个。
https://github.com/Livin21/MissMe https://github.com/skydoves/ProgressView
这些都是完美的,而且是根据需要来做的。
也许这个指南可以帮助你。
通常我更喜欢用指示器制作自定义alertdialog。它解决了诸如自定义App视图等问题。