Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.lang.reflect.Method;

import android.view.View;

public class Main {
    /** Adds a click listener to the given view which invokes the method named by methodName on the given target.
     * The method must be public and take no arguments.
     */
    public static void bindOnClickListener(final Object target, View view, String methodName) {
        final Method method;
        try {
            method = target.getClass().getMethod(methodName);
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
        view.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    method.invoke(target);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
    }
}