Android DialogFragment进度条

发布于 2024-12-03 09:21:10 字数 439 浏览 0 评论 0原文

我到处搜索过,但找不到解决这个问题的方法。

基本上我有一个登录屏幕,我试图在登录服务器(通过线程)时显示进度旋转器,然后在登录成功后将其关闭。它必须在改变方向时发挥作用。

我正在使用 DialogFragment 和 Android 兼容包来制作进度条(找不到任何相关文档,仅适用于基本\警报对话框),因为 showDialog() 现在已弃用。现在我只是显示一个自定义消息框作为登录微调器。

总结:

  • 如何使用 DialogFragment 设置进度微调器。
  • 方向更改后如何在另一个线程中将其关闭。

I've searched everywhere and I can't find a solution to this problem.

Basically I have a login screen and I'm trying to get a progress spinner to show up while it's logging in to the server (via a thread), and then dismiss it after the login is successful. It has to work while changing orientations.

I am using DialogFragment with the Android compatibility package to make a progress bar (can't find any documentation on it, only for basic\alert dialog) because showDialog() is deprecated now. Right now I just show a custom message box as a login spinner.

In Summary:

  • How can I set up a Progress spinner with DialogFragment.
  • How can I dismiss it in another thread after orientation changes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们只是彼此的过ke 2024-12-10 09:21:10

要显示进度微调器,只需覆盖 DialogFragment .onCreateDialog() 在您的对话框片段中,如下所示(无需覆盖 onCreateView()):

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    final ProgressDialog dialog = new ProgressDialog(getActivity());
    //
    dialog.setTitle(R.string.login_title);
    dialog.setMessage(getString(R.string.login_message));
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    // etc...
    return dialog;
}

至于从其他地方消除该对话框片段,您需要获得一个持有 FragmentManager (从下一个 FragmentActivityFragment 内部)并调用 popBackStack()(如果你没有在其中进行任何其他片段事务)其间)。

如果进度对话框片段和下一个活动之间存在更多步骤/片段事务,您可能需要其他 popBackStack(...) 方法之一,该方法采用 ID 或标记来弹出所有内容将进度对话框片段从堆栈中删除。

For showing a progress spinner, just override DialogFragment.onCreateDialog() in your dialog fragment like this (no need for overriding onCreateView()):

@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
    final ProgressDialog dialog = new ProgressDialog(getActivity());
    //
    dialog.setTitle(R.string.login_title);
    dialog.setMessage(getString(R.string.login_message));
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);
    // etc...
    return dialog;
}

As for dismissing that dialog fragment from somewhere else, you'll need to get a hold of FragmentManager (from inside your next FragmentActivity or Fragment) and call popBackStack() on it (if you don't do any other fragment transaction in the meantime).

If there's more steps/fragment transactions between your progress dialog fragment and the next activity, you'll probably need one of the other popBackStack(...) methods that take an ID or tag to pop everything up to your progress dialog fragment off the stack.

独闯女儿国 2024-12-10 09:21:10

我知道这是一个老问题,但我想

根据 Android Development Protip 分享更好的解决方案:

“停止使用 ProgressDialog,内联指示器是你的朋友”

正如 Roman Nurik 所说:

在此处输入图像描述

这个速度很快。停止使用 ProgressDialog 和其他模式加载
指标。他们非常打扰和烦人,尤其是
什么时候:

  • 每次切换标签页时您都会看到一个标签页。

  • 你无法退出它们。

  • 他们说“请稍等。”不,谢谢,我宁愿卸载。

要么显示与您的内容一致的加载指示器(例如
http://developer.android.com/training/animation/crossfade.html )或者更好的是,在中加载少量数据
背景,以便您最大限度地减少甚至显示加载的需要
指标。

有关进展和进展的更多信息设计指南中的活动。

I know this is old question but I want to share much better solution for this

According to Android Development Protip:

"Stop using ProgressDialog,Inline indicators are your friend"

As Roman Nurik states:

enter image description here

This one's quick. Stop using ProgressDialog and other modal loading
indicators. They're extremely interruptive and annoying, especially
when:

  • You see one every time you switch tabs.

  • You can't Back out of them.

  • They say "Please wait." No thanks, I'd rather just uninstall.

Either show loading indicators inline with your content (e.g.
http://developer.android.com/training/animation/crossfade.html) or better yet, load small amounts of data in the
background so that you minimize the need to even show a loading
indicator.

More about progress & activity in the design guidelines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文