自定義按鈕我有一個自定義按鈕,上我捕捉它的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