不推荐使用ProgressDialog

时间:2017-07-27 12:41:30

标签: android progressdialog android-8.0-oreo

由于从Android版本O弃用了ProgressDialog,我仍然找到了更好的方法来完成我的任务。任务是从我的活动转移到片段。一切都工作正常,但progressdialog不可见。我尝试过实施它但是... progressdialog不起作用。

似乎进度条可以正常工作但仍无法正常工作。我需要一个progressdialog,因为我很容易设置我的标题和消息。我需要一个微调器progressDialog,但不知道该怎么做。这是我的一项工作,但没有实施:

Java类

ublic class SaveVideo extends AppCompatActivity {

private Button button;

private ProgressDialog mProgressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_save_video);

    mProgressDialog = new ProgressDialog(this);

    getSupportActionBar().setHomeAsUpIndicator(R.drawable.back);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    button = (Button) findViewById(R.id.saveVideo);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //where it must be seen when the button is pressed
            mProgressDialog.setTitle("Title");
            mProgressDialog.setMessage("Message");
            mProgressDialog.show();

            Intent intent = new Intent(SaveVideo.this,MainActivity.class);
            intent.putExtra("change",2);
            startActivity(intent);

            //as soon as the page moves from this to another fragment
            mProgressDialog.dismiss();
        }
    });


}

我是Android版O的新手。任何帮助都会给我新的东西!

9 个答案:

答案 0 :(得分:14)

正如在Android O文档中提到的那样:

  

此类在API级别26中已弃用.ProgressDialog是一种模式   对话框,阻止用户与应用程序交互。代替   使用这个类,你应该使用像这样的进度指示器   ProgressBar,可以嵌入到您应用的UI中。或者,   您可以使用通知来通知用户任务的进度。

您可以使用TextView和ProgressBar创建自定义视图并管理其可见性。 您也可以使用this library,因为它使用的是AlertDialog而不是ProgressDialog。

答案 1 :(得分:5)

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();

现在剩下的就是在单击事件中显示和隐藏此对话框 像这样

progress_dialog.show(); // to show this dialog
progress_dialog.dismiss(); // to hide this dialog

就这样,它应该可以工作,因为您可以看到,实现ProgressBar(例如ProgressDialog)而不是不推荐使用的ProgressDialog非常简单容易。 现在您可以在Handler或ASyncTask中显示/关闭此对话框,具体取决于您的需要,希望您可以使用它来解决问题,欢呼

答案 2 :(得分:2)

是的,API level 26已弃用,最好只能使用progressbar

使用此代码段以编程方式创建:

ProgressBar progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleSmall);

仅供将来参考,请将android.R.attr.progressBarStyleSmall更改为android.R.attr.progressBarStyleHorizontal

也许这guide可以帮到你。

我希望这对你有所帮助。

答案 3 :(得分:1)

您可以使用ProgressBar代替ProgressDialog。 使用ProgressBar和您需要的其他小部件在自定义对话框中创建TextView

答案 4 :(得分:1)

此类已在API级别26中弃用。

  

ProgressDialog是一个模式对话框,可以防止用户进入   与应用程序交互。您应该使用而不是使用此类   一个进度指示器,如ProgressBar,可以嵌入你的   应用程序的UI。或者,您可以使用通知来通知用户   任务的进展。

https://developer.android.com/reference/android/app/ProgressDialog.html

答案 5 :(得分:1)

您需要在其上创建一个包含ProgressBar的自定义XML布局文件,并显示该文件。我一直在使用像https://github.com/Q115/DelayedProgressDialog这样的库来实现这个简单的行为。

用法:

DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");

答案 6 :(得分:0)

这是我设法整理的内容,因为该类已在Android Oreo(API 26 +)中弃用。

在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:visibility="gone"
            android:layout_marginTop="5dp"
            android:id="@+id/top_progressBar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_height="wrap_content"
            android:indeterminateTintMode="src_in"
            android:indeterminateTint="@color/white"
            android:indeterminate="true" />
    <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" />
  <ProgressBar
                android:visibility="gone"
                android:layout_marginTop="2dp"
                android:id="@+id/down_progressBar"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp"
                android:layout_height="wrap_content"
                android:indeterminateTintMode="src_in"
                android:indeterminateTint="@color/white"
                android:indeterminate="true" />

</LinearLayout>

在上面的示例中,我想到了一个滚动情况,说你的视图很长,因此有两个进度条。

在Java文件示例中:

public class GetUserDetails extends AppCompatActivity {
private ProgressBar topProgressBar, downProgressBar;
    private ProgressDialog progressDialog;
@Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_get_user_details );
//initilise the progressbar views and progress dialog object
 topProgressBar = findViewById ( R.id.top_progressBar );
    downProgressBar = findViewById ( R.id.down_progressBar );
  if ( Build.VERSION.SDK_INT < 26 ) {
        progressDialog = new ProgressDialog ( this );
    } else {
        topProgressBar.setIndeterminate ( true );
        downProgressBar.setIndeterminate ( true );
    }
 }



    private void showProgressDialog (final boolean isToShow) {
            if ( Build.VERSION.SDK_INT < 26 ) {
                if ( isToShow ) {

                    progressDialog.setMessage ( "Processing ...Please wait." );
                    progressDialog.setCancelable ( false );
                    if ( ! progressDialog.isShowing () ) {
                        progressDialog.show ();

                    }

                } else {
                    if ( progressDialog.isShowing () ) {
                        progressDialog.dismiss ();
                    }
                }
            } else {
                /* this is Android Oreo and above*/
                if ( isToShow ) {
                    topProgressBar.setVisibility ( View.VISIBLE );
                    downProgressBar.setVisibility ( View.VISIBLE );
                    getWindow ().setFlags ( WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE );
                } else {
                    topProgressBar.setVisibility ( View.GONE );
                    downProgressBar.setVisibility ( View.GONE );
                    getWindow ().clearFlags ( WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE );
                }
            }
        }

}

嗯,这是我的黑客,所以我希望它有所帮助。

答案 7 :(得分:0)

如果有人坚持要有进度对话框,在我的情况下,我在警告对话框中选择了进度条。您可以使用以下代码开始。

我的案子很简单,因为我只需要一个不确定的进度条。对于完整版本,您必须将其封装到类中并访问Bar。

private AlertDialog Create_Indeterminate_HorizontalProgressBar_AlertDialog(
        Context context, String title, String message)
{
    final ProgressBar progressBar =
            new ProgressBar(
                    context,
                    null,
                    android.R.attr.progressBarStyleHorizontal);

    progressBar.setLayoutParams(
            new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));

    progressBar.setIndeterminate(true);

    final LinearLayout container =
            new LinearLayout(context);

    container.addView(progressBar);

    int padding =
            getDialogPadding(context);

    container.setPadding(
            padding, (message == null ? padding : 0), padding, 0);

    AlertDialog.Builder builder =
            new AlertDialog.Builder(context).
                    setTitle(title).
                    setMessage(message).
                    setView(container);

    return builder.create();
}

private int getDialogPadding(Context context)
{
    int[] sizeAttr = new int[] { android.support.v7.appcompat.R.attr.dialogPreferredPadding };
    TypedArray a = context.obtainStyledAttributes((new TypedValue()).data, sizeAttr);
    int size = a.getDimensionPixelSize(0, -1);
    a.recycle();

    return size;
}

注意:如果你想知道为什么栏位于一个容器中:我只是无法让栏杆在栏上工作而不得不放入容器。

答案 8 :(得分:-2)

您的示例中的ProgressDialog无法显示,因为您在dismiss()之后立即致电show()。创建一个Intent并且对startActivity()的调用没有阻塞:基本上你只是安排切换到要执行的其他活动&#34;很快&#34;。

您必须将dismiss()来电转移到您的活动onStop:

@Override
protected void onStop()
{
    super.onStop();
    mProgressDialog.dismiss();
}

此外,有人可能会问:为什么在这种情况下从一项活动转到另一项活动需要这么长时间?我猜你的MainActivity在onCreate / onStart / onResume方法上做了一些繁重的工作。一种更好的处理方式可能是将所有工作放入一个单独的线程中。