我发现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。ProgressDialog的外观可以通过在AlertDialog中放置ProgressBar来复制。
你仍然可以使用它,但Android不希望你使用它,这就是它被弃用的原因。所以你应该考虑用另一种方式解决你的问题,比如在你的布局中嵌入一个ProgressBar。
其他回答
你可以使用wasabeef库来使用SpotDialog,你可以从下面的链接找到完整的教程:
Android中的SpotsDialog示例
下面是@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)
)
}
}
好吧,如果你真的想违背他们的意愿,你仍然可以使用甜蜜警报对话框或自己创建一个。
progress_dialog_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="64dp" >
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:gravity="center|left"
android:id="@+id/textView9"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="@color/black"
android:textSize="18sp"
android:text="Downloading data. Please wait.." />
</TableRow>
</RelativeLayout>
Java代码:
AlertDialog b;
AlertDialog.Builder dialogBuilder;
public void ShowProgressDialog() {
dialogBuilder = new AlertDialog.Builder(DataDownloadActivity.this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View dialogView = inflater.inflate(R.layout.progress_dialog_layout, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setCancelable(false);
b = dialogBuilder.create();
b.show();
}
public void HideProgressDialog(){
b.dismiss();
}
您可以简单地为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作为参数来取消对话框。
ProgressBar是ProgressDialog的最佳选择。 指示操作进度的用户界面元素。
欲了解更多信息,请参阅谷歌文档:https://developer.android.com/reference/android/widget/ProgressBar.html