我试图建立简单的库按钮,库文件夹下面我创建类
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
}
}
我在app文件夹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功能,在点击按钮应直接重定向到SimpleImageButton类onclcik方法
我是很新的堆栈溢出,如果问的问题的语法/方法的任何错误,请别介意。谢谢。
使用setOnClickListener(这个)你的看法的构造函数中。
public SimpleImageButton (Context context) {
super(context);
mContext = context;
setCustomTypeface(context, null);
setOnClickListener(this);
}