0

Is there a preferred way to show 'loading' animations in Android? Currently I'm showing Dialog with the text "Loading..." for long running processes. It's tricky to get a .gif to work for this, so I'm wondering if this is a problem that has been solved before, and if so, how?

4

5 回答 5

3

在我的应用程序中,我通常使用 aProgressDialog来显示旋转的“正在加载...”消息。有更漂亮/更漂亮的方法,但这是一个快速简便的内置解决方案。

ProgressDialog progressDialog = ProgressDialog.show(Activity.this, "",
                        "Loading...");

...做一些工作...

progressDialog.dismiss();

progressDialog = null;

如果您有一个静态实用程序类来生成警报和对话框等内容,这里有 2 个不错的补充:

public static ProgressDialog createProgressDialog(Context context, String title, String message, boolean isCancelable)
{
    ProgressDialog dialog = new ProgressDialog(context);
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setCancelable(isCancelable);

    return dialog;
}

如果您在后台使用s 进行工作,则在显示和关闭 s 时检查 s 是否仍然“活动”并且未完成AsyncTask尤其重要,否则您的应用程序将因奇怪的异常而死(请参阅此答案) .ActivityProgressDialog

public static void safeShowProgressDialog(Context context, ProgressDialog dialog)
{
    if(!((Activity) context).isFinishing())
    {
        dialog.show();
    }
}

与关闭对话框相同:

public static void safeDismissProgressDialog(Context context, ProgressDialog dialog)
{
    if(!((Activity) context).isFinishing())
    {
        dialog.dismiss();
    }
}

请参阅 API 参考:

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

于 2013-08-14T15:07:49.167 回答
1

这是一个很好的例子:

假设您尝试登录并等待来自服务器的响应。在等待您显示进度对话框和成功切换登录活动到主要:

    private ProgressDialog dialog;

   ...
 dialog = ProgressDialog.show(FirstLoginActivity.this, "", "Connecting. Please wait...", true);
    HeavyTask task = new HeavyTask();
    task.execute(str1, str2);   

private class HeavyTask extends AsyncTask<String, Void, Void> {

    private String str1= "";
    private String str2= "";



    protected Void doInBackground(String... args) {

        str1= args[0];
        str2= args[1];


        try {
            doSomething(str1, str2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(Void results) {

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                doStuff(str1, str2);                    
            }
        }, 500);            
    }   

private void doStuff(String str1, String str2) {
  mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {

                                startActivity(new Intent().setClass(FirstLoginActivity.this, OtherActivity.class).setData(getIntent().getData()));
                                Toast.makeText(getApplicationContext(), "Registration succeeded.", Toast.LENGTH_LONG).show();
                                //kill the dialog waiting
                                dialog.dismiss();
                                dialog = null;


                            }
                        }, 1000);

}
于 2013-08-14T15:15:06.797 回答
0

您可以在 Android 设计文档中找到一些指导:http: //developer.android.com/design/building-blocks/progress.html

于 2013-08-14T15:09:13.017 回答
0

你可以使用进度条。我通常通过 asyctasks 使用它。并将以下方法重载为:

protected void onProgressUpdate(Integer... progress) {
((ProgressBar) findViewById(R.id.progressBar1)).setProgress(progress[0]);
}
于 2013-08-14T15:12:48.513 回答
0

根据您的应用程序设计,它会有所不同,但 a ProgressDialog是一个相当常见的解决方案。本教程将向您展示如何将这一切联系在一起以及两种变体(显示或不显示进度)。

于 2013-08-14T15:13:33.403 回答