Create an onclick-listeners for multiple views - Android User Interface

Android examples for User Interface:View Click

Description

Create an onclick-listeners for multiple views

Demo Code


//package com.java2s;
import android.app.Activity;

import android.view.View;

public class Main {
    /**//ww w  .  j a va  2 s  . c  om
     * Create an onclick-listeners for multiple views
     *
     * @param onClickListener onclickListener for views
     * @param views           views
     */
    public static void setOnClickListenerForViews(
            View.OnClickListener onClickListener, View... views) {
        for (View v : views) {
            v.setOnClickListener(onClickListener);
        }
    }

    /**
     * Create an onclick-listeners for multiple views
     *
     * @param rootView        Root view containing the layout
     * @param onClickListener onclickListener for views
     * @param viewsIds        view IDs
     */
    public static void setonClickListenerForViews(View rootView,
            View.OnClickListener onClickListener, int... viewsIds) {
        for (int viewsId : viewsIds) {
            rootView.findViewById(viewsId).setOnClickListener(
                    onClickListener);
        }
    }

    /**
     * Create an onclick-listeners for multiple views by ID
     *
     * @param activity        Activity containing the layout
     * @param onClickListener onclickListener for views
     * @param viewsIds        view IDs
     */
    public static void setonClickListenerForViews(Activity activity,
            View.OnClickListener onClickListener, int... viewsIds) {
        for (int viewsId : viewsIds) {
            activity.findViewById(viewsId).setOnClickListener(
                    onClickListener);
        }
    }
}

Related Tutorials