Pri*_*ria 5 android gridview onclick custom-controls touch
我有一个自定义按钮,我正在捕获它的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;
}
Run Code Online (Sandbox Code Playgroud)
此自定义按钮是网格的一部分.当我将此按钮添加到网格时,我已经为它设置了一个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;
}
Run Code Online (Sandbox Code Playgroud)
请帮忙.
小智 7
在你的onTouchEvent实现中,而不是"return true;",做...
return super.onTouchEvent(event);
Run Code Online (Sandbox Code Playgroud)
你覆盖了超类的实现,这是负责调用监听器的.通过调用超类的实现,它应该像以前一样.这就是为什么你的代码在注释掉方法时工作的原因 - 因为你不再覆盖超类的实现
我想 Mathias 的评论是正确的,当您希望触发 onClick() 事件侦听器而不是后续的 onTouch() 事件侦听器时,您必须在 onTouchEvent 方法中返回 false。
您可以找到更精确的 int UI Events 文档
| 归档时间: |
|
| 查看次数: |
5087 次 |
| 最近记录: |