问题描述
我有一个自定义按钮,在其上我捕捉它的onTouchEvent。
I have a custom button, on which I am capturing its onTouchEvent.
public class CustomNumber extends ToggleButton {
boolean drawGlow = false;
float glowX = 0;
float glowY = 0;
float radius = 30;
public CustomNumber(Context context) {
super(context);
}
public CustomNumber(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomNumber(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
Paint paint = new Paint();
{
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setAlpha(70);
};
@Override
public void draw(Canvas canvas){
super.draw(canvas);
if(drawGlow)
canvas.drawCircle(glowX, glowY, radius, paint);
}
@Override
**public boolean onTouchEvent(MotionEvent event)**{
if(event.getAction() == MotionEvent.ACTION_DOWN){
drawGlow = true;
}else if(event.getAction() == MotionEvent.ACTION_UP)
drawGlow = false;
glowX = event.getX();
glowY = event.getY();
this.invalidate();
return true;
}
}
这个自定义按钮是网格的一部分。当我加入这个按钮到网格,我设置了OnClickListener它。但是,在code在OnClickListener是永远不会被调用。
This custom button is the part of a grid. When I am adding this button to the grid, I have set a OnClickListener to it. But, the code in the OnClickListener is never invoked.
//电网适配器code,其中我将与听众的按钮。
//Grid Adapter code, where I am adding the button with listener.
public View getView(final int position, final View convertView, final ViewGroup parent) {
CustomNumber tBtn;
if (convertView == null) {
tBtn = new CustomNumber(context);
tBtn.setTextOff("");
tBtn.setTextOn("");
tBtn.setChecked(false);
tBtn.setId(position);
tBtn.setOnClickListener(tBtnListener);
tBtn.setLayoutParams(new GridView.LayoutParams(35, 35));
} else {
tBtn = (CustomNumber) convertView;
}
return tBtn;
}
请帮忙。
推荐答案
,而不是在你的onTouchEvent实施,返回true;做......
In your onTouchEvent implementation, instead of "return true;", do...
返回super.onTouchEvent(事件);
您要覆盖父类的实现,它是负责调用监听器。通过调用父类的实现,应该采取行动,因为它以前那样。这就是为什么你的code,当你注释掉方法的工作 - 因为你不再重写超实施
You're overriding the superclass' implementation which is what is responsible for calling the listener. By calling the superclass' implementation it should act as it did before. This is why your code works when you comment out the method - because you're no longer overriding the superclass' implementation
这篇关于安卓:自定义按钮,OnClickListener是没有得到调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!