【发布时间】:2010-08-06 09:05:57
【问题描述】:
我有一个自定义按钮,我正在捕获它的 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。但是,OnClickListener 中的代码永远不会被调用。
GridAdapter 代码,我在其中添加带有监听器的按钮:
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 代码,则 OnClickListener 代码可以工作。两个事件不能一起处理???请帮忙!
-
不确定,但最后在 onTouch 中返回 true 还是 false 有什么区别?
-
我试图从 onTouchEvent() 返回 false。它不起作用:(此外,它始终将事件视为 MotionEvent.ACTION_DOWN
标签: android gridview custom-controls onclick touch