当进度条在android中旋转时如何停止改变方向

发布于 2024-12-09 11:14:00 字数 161 浏览 0 评论 0原文

我有一个注册屏幕,用户在其中输入所有注册详细信息,当用户单击“注册”按钮时,我显示进度条(进度条开始旋转),然后将用户带到主屏幕。如果用户旋转当进度条旋转时,手机会关闭进度条。当进度条可见时(意味着它正在旋转),我希望方向不要从纵向更改为横向,反之亦然。如何在进度条时停止方向更改正在旋转?有什么帮助吗赞赏。

I have a Registration screen where user enters all the registration details and when user clicks on the "Register" button i am showing the progress bar(Progress bar begins to spin) and then takes the user to the home screen.If the user rotates the phone when progress bar is spinning, the progress bar gets dismissed.I want the orientation not to change from potrait to landscape and vice versa,when a progress bar is visible(meaning it is spinning).How to stop the orientation change when progress bar is spinning?Any help would be appreciated.

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

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

发布评论

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

评论(3

撑一把青伞 2024-12-16 11:14:00

发生这种情况是因为当屏幕方向旋转时,活动会重新启动。在这种情况下,您可以在 AndroidManifest 文件的标记中添加 configChanges 属性来停止重新创建 Activity。

<activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">

这样,如果进度条正常工作,您的方向就会改变,而且即使方向改变,它也不会停止。

更新

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.login_landscape);
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.login);         
        }
    }

This is happening because when screen orientation rotates the Activity gets re-started. In this case you can add configChanges attribute in your tag in the AndroidManifest file to stop the re-creation of the Activity.

<activity android:name=".Activity_name"
          android:configChanges="orientation|keyboardHidden">

By, this your orientation can change will the progress bar is working and also it won't stop though the orientation changes.

UPDATE

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
            setContentView(R.layout.login_landscape);
        }
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            setContentView(R.layout.login);         
        }
    }
帝王念 2024-12-16 11:14:00

正如此处所解释的,调用

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

然后

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

真正就像魅力一样...在真实设备上!

不要认为在模拟器上测试时它已损坏,ctrl+F11 快捷键始终会更改屏幕方向,而不模拟传感器移动。

其他一些参考:

  1. 以编程方式在 Android 中启用/禁用屏幕旋转
  2. Android 子活动和方向锁定

这是Activityinfo

************* **** 已编辑 ********************************

我认为你无法处理进度条在屏幕旋转上。 我说得对吗?

因此,您无需为此解决方案停止轮换。您应该处理进度条。
这里有一些教程,对于此目的很有用..

  1. 当进度对话框和后台线程处于活动状态时如何处理屏幕方向变化?
  2. 处理进度对话框和方向更改
  3. 主题和Android 屏幕方向旋转中的进度对话框
  4. Android 持久 ProgressDialog 正确完成

************** 已编辑 2 ******************

有时,当方向改变时,您会丢失数据。因此,您可以尝试这些教程

  1. 处理运行时更改
  2. 更快的屏幕方向更改
  3. 处理 Android 中的方向变化

As explained here, calling

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

and then

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

really works like charm... on real devices !

Don't think that it's broken when testing on the emulator, the ctrl+F11 shortcut ALWAYS change the screen orientation, without emulating sensors moves.

Some other references :

  1. Programatically enabling/disabling screen rotations in Android
  2. Android child activity and orientation lock

And here is Activityinfo

***************** Edited ********************************

And I think you are not able to handle progressbar on screen rotation. Am I right?

So you need not to stop the rotation for this solution. You should handle the progress bar.
Here are some tutorials, which are useful for this purpose ..

  1. How to handle screen orientation change when progress dialog and background thread active?
  2. Handling progress dialogs and orientation changes
  3. Threads and Progress Dialogs in Android Screen Orientation Rotations
  4. Android Persistent ProgressDialog Done Right

************** Edited 2 *****************

And some time you looses data when orientation changes. So you can try these tutorials

  1. Handling Runtime Changes
  2. Faster Screen Orientation Change
  3. Handling Orientation Change in Android
歌枕肩 2024-12-16 11:14:00

在 Activity 类中使​​用 setRequestOrientation 方法。

继承ProgressDialog类,并重写方法。
示例代码如下。

    class myDialog extends ProgressDialog
{
    Activity mActivity;
    public myDialog(Activity context) {
        super(context);
        this.mActivity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    }

    @Override
    public void dismiss() {            
        super.dismiss();
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }
}

use setRequestOrientation method in Activity class.

Inherit ProgressDialog class, and override methods.
Example code is below.

    class myDialog extends ProgressDialog
{
    Activity mActivity;
    public myDialog(Activity context) {
        super(context);
        this.mActivity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
    }

    @Override
    public void dismiss() {            
        super.dismiss();
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文