在Android自定义视图中设置onClickListener

在Android自定义视图中设置onClickListener,android,android-studio,android-button,android-library,Android,Android Studio,Android Button,Android Library,我正在尝试为button构建简单的库, 在我在类下面创建的库文件夹中 public class SimpleImageButton extends AppCompatImageView implements AppCompatImageView.OnClickListener{ public Context mContext; Activity activity; public SimpleImageButton (Context context) {

我正在尝试为button构建简单的库, 在我在类下面创建的库文件夹中

 public class SimpleImageButton extends AppCompatImageView implements AppCompatImageView.OnClickListener{

    public Context mContext;
    Activity activity;

    public SimpleImageButton (Context context) {
        super(context);
        mContext = context;
        setCustomTypeface(context, null);

    }

    public SimpleImageButton (Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        setCustomTypeface(context, attrs);

    }

    public SimpleImageButton (Context context, AttributeSet attrs, int defStyleAttr) 
    {
        super(context, attrs, defStyleAttr);
        mContext = context;
        setCustomTypeface(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setCustomTypeface(Context context, AttributeSet attrs)  {
        if(isInEditMode())
            return;
        TypedArray a = context.obtainStyledAttributes(attrs, 
        android.support.v7.appcompat.R.styleable.TextAppearance);
        setBackground(ContextCompat.getDrawable(mContext, 
    R.drawable.applogo_ads));
        a.recycle();

    }
   public void onClick(View view) {
        // here i have some functions to execute
    }
}
和我的应用程序文件夹中的Mainclass

   SimpleImageButton imgBtn= (SimpleImageButton )findViewById(R.id.clickButton);

imgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               imgBtn.onClick(view);
              // without this line how can i reach to onclick() of simpleImageButton class
            }
        });
所以,当我点击按钮时,一切都很好。但我想让库按钮直接工作,而不需要在主活动中使用onClick函数,点击按钮应该直接重定向到SimpleMapgeButton类onclcik方法

我对堆栈溢出非常陌生,如果在语法/提问方式上有任何错误,请不要介意。 谢谢。

在视图的构造函数中使用setOnClickListener(this)

公共SimpleImage按钮(上下文){
超级(上下文);
mContext=上下文;
setCustomTypeface(上下文,空);
setOnClickListener(此);
}

在库中使用回调侦听器。。一旦点击监听器,只需将呼叫发送回父活动或片段,我想实现这样的效果:我不应该在主活动中为点击操作写任何东西!!!!!!单击该按钮后,它应该直接执行SimpleImageButton类onclick()方法中的函数。此外,您还需要能够在使用者中设置click侦听器。以下是方法: