Disable a whole activity from user action

后端 未结 7 646
再見小時候
再見小時候 2020-12-02 09:17

Is there a simple way to disable a user interacting with an activity. To be done when there is an action running (and a spinning progress bar in the title bar)

EDIT:

相关标签:
7条回答
  • 2020-12-02 09:37

    I solved this by adding an full screen blank image with a null click handler at the end on the XML layout.

    Create a transparent PNG file in the drawables folder.

    Add a full screen ImageView at the end of the XML layout referencing the image:

    ...
     <ImageView
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         app:srcCompat="@drawable/img_mask"
         android:id="@+id/imgMask"
         android:scaleType="fitXY"
     android:visibility="gone"
         android:onClick="doNothingClick"
     />
    

    Add the null click hander for the image to capture user touches:

    public void doNothingClick(View v){
        // do nothing with the click
    }
    

    Add the code to disable the user touches:

    private void StopTouches(int duration){
        findViewById(R.id.imgMask).setVisibility(View.VISIBLE);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                findViewById(R.id.imgMask).setVisibility(View.GONE);
            }
        }, duration);
    }
    
    0 讨论(0)
  • 2020-12-02 09:40

    In order to block user touch events, use:

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
    WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    

    To get touch events back, use:

    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
    

    EDIT: If you want to add a feature of disable and greyed out display, you need to add in your xml layout file a linear layout that fills the parent. Set its background to #B0000000 and its visibilty to Gone. Than programicly set its visibility to Visible.

    0 讨论(0)
  • 2020-12-02 09:49

    If you need to disable event processing for a period of time (for instance, while you run an animation, show a waiting dialog), you can override the activity's dispatch functions.

    To disable touch/clicks on any buttons, add these members/functions to your activity:

    protected boolean enabled = true;
    
    public void enable(boolean b) {
        enabled = b;
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return enabled ? 
            super.dispatchTouchEvent(ev) : 
            true;        
    }
    

    Then just call enable(true/false) when you need to disable and enable the activity's normal event handling.

    0 讨论(0)
  • 2020-12-02 09:49

    I have solved this using a custom method - which I did not want to do:

    public static void setViewGroupEnabled(ViewGroup view, boolean enabled)
        {
            int childern = view.getChildCount();
    
            for (int i = 0; i< childern ; i++)
            {
                View child = view.getChildAt(i);
                if (child instanceof ViewGroup)
                {
                    setViewGroupEnabled((ViewGroup) child, enabled);
                }
                child.setEnabled(enabled);
            }
            view.setEnabled(enabled);
        }
    

    If anyone finds a better way I'd like to hear about it! Thanks.

    0 讨论(0)
  • 2020-12-02 09:51

    Use AsyncTask with ProgressDialog bundled.

    1. AsyncTask

    2. Progress Dialog

    another useful example:

    http://www.screaming-penguin.com/node/7746

    0 讨论(0)
  • 2020-12-02 09:56

    The code of Uriel Frankel (accepted response), works good, but in my case works after my request it's done :(. I want to block before it happend. Some one knows what is wrong in my code (I'm beginning in this..)

    (this is a fragment)

       login_login_btn.setOnClickListener {
    
        if (validateInputs()){
            showSpinner()
            thread {
                doLogin()
            }
        } else {
            validationError("Validation Error","Checkout your inputs. Common errors: \npassword (at least 8 characters)")
        }
    
    }
    

    }

    Function after OnViewCreated

    fun showSpinner(){
        activity?.window?.setFlags(
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
        spinner.visibility = View.VISIBLE
    }
    

    Thank you (Y)

    0 讨论(0)
提交回复
热议问题