自定义按钮我有一个自定义按钮,上我捕捉它的onTouchEvent。安卓:OnClickListener是没有得到调用
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代码工作。不能同时处理这两个事件?请帮忙! – Pria 2010-08-06 09:08:32
不确定,但是在最后的onTouch中是否返回true或false会有什么区别? – 2010-08-06 09:27:38
我试图从onTouchEvent()返回false。它不工作:(此外,它采用事件作为MotionEvent.ACTION_DOWN总是 – Pria 2010-08-06 10:08:36