Android app prevents users from clicking multiple times in a short time

In daily development, the app must deal with the user’s continuous click problem, because multiple repeated clicks within a short period of time will be repeated regardless of whether it is a network request or data submission. Although the network request can also be set to filter repeated submissions, when you Click a button to open an activity, and you will open multiple ones. It is useless to set the startup mode of the activity. If you remember Android's event distribution mechanism, you will find it very simple.

1 android event distribution borrowing a picture on the network, you can find that the event distribution has a U-shaped trend, no matter what the event, it is distributed from the dispatchTouchEvent () of the Activity, you need to pay attention to the return value of the event distribution, false will also be Consumer .

/*
    * 防止短时间内多处点击
    * */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN){
            if (isClick()){
                return super.dispatchTouchEvent(ev);
            }else {
                return true;
            }
        }

        return super.dispatchTouchEvent(ev);
    }

/**
     * 点击1秒内不能连续点击
     */
    public boolean isClick() {
        if ((System.currentTimeMillis() - exitTime) > EXITTIME) {
            exitTime = System.currentTimeMillis();
            return true;
        } else {
            return false;
        }
    }

Summary: In the BaseActivity of your project, override dispatchTouchEvent. If it is a down event, judge the current click time and the last click time. I set it to 500ms. The point is that if the click event can be dispatched, you need to return to super .

Guess you like

Origin blog.csdn.net/plx_csdn/article/details/115260108