How to disable user interaction while ProgressBar is visible in android?

后端 未结 7 2041
我在风中等你
我在风中等你 2020-12-12 13:08

I am using a custom ProgressBar. Now while a task is going on, I am showing the progress bar, but user can still interact with the views and controls. How do I disable the

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 13:31

    Make a dialog with transparent background. The issue with getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE); is that when app will go in background and come back user will be able to interact with UI components, a lot more handling. So for blocking UI make a transparent dialog and if you want to set time for hide/show. Do this in a runnable thread. So the solution will be

    public class TransparentDialogHelper {
    
        private Dialog overlayDialog;
    
        @Inject
        public TransparentDialogHelper() {
    
        }
    
        public void showDialog(Context context) {
            if (AcmaUtility.isContextFinishing(context)) {
                return;
            }
            if (overlayDialog == null) {
                overlayDialog = new Dialog(context, android.R.style.Theme_Panel);
                overlayDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_UNCHANGED);
            }
            overlayDialog.show();
        }
    
        public void hideDialog() {
            if (overlayDialog == null || AcmaUtility.isContextFinishing(overlayDialog.getContext())) {
                return;
            }
            overlayDialog.cancel();
        }
    }
    
    -------- Timer
    
    Handler handler = new Handler();
    handler.postDelayed( () -> {
        view.hideProgress();
    }, 2000);
    

提交回复
热议问题