Remove an onclick listener from View - Android User Interface

Android examples for User Interface:View Click

Description

Remove an onclick listener from View

Demo Code

/**//from www  . jav  a 2  s  .c  o  m
 * Copyright (C) BlakeQu All Rights Reserved <blakequ@gmail.com>
 * <p/>
 * Licensed under the blakequ.com License, Version 1.0 (the "License");
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * <p/>
 * author  : quhao <blakequ@gmail.com>
 * date     : 2016/4/26 10:58
 * last modify author :
 * version : 1.0
 * description:????????
 */
//package com.java2s;

import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

public class Main {
    /**
     * Remove an onclick listener
     * @param view the root view of the layout
     */
    public static void unBindListener(View view) {
        if (view != null) {
            try {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
                    if (view.hasOnClickListeners()) {
                        view.setOnClickListener(null);
                    }
                } else {
                    view.setOnClickListener(null);
                }

                if (view instanceof ViewGroup
                        && !(view instanceof AdapterView)) {
                    ViewGroup viewGroup = (ViewGroup) view;
                    int viewGroupChildCount = viewGroup.getChildCount();
                    for (int i = 0; i < viewGroupChildCount; i++) {
                        unBindListener(viewGroup.getChildAt(i));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Related Tutorials