0
我做了一个自定义按钮,这是一个奇怪的形状,所以我忽略了`dispatchTouchEvent。 然后,我添加了一个onTouch侦听器,以便在触摸时更改图像。 onClick属性停止工作,因此我添加了onClick监听器。 现在onClick侦听器或onClick属性都没有触发 有人知道为什么吗?我似乎无法解决这个问题。自定义ImageButton视图
这是自定义视图:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
double iX = (double) event.getX();
double iY = (double) event.getY();
String isLeft = (String) getContentDescription();
double Ymax;
if(isLeft.equals("left"))
Ymax = Math.sqrt((this.getWidth()*getWidth()) - ((getWidth() - iX)*(getWidth() - iX)) + (getHeight()*1.2));
else
Ymax = Math.sqrt((this.getWidth()*getWidth()) - ((iX)*(iX)) + (getHeight()*1.2));
if (!(iY>Ymax || iY<0.0 || iX>getWidth() || iX<0.0)) {
return false;
}
return super.dispatchTouchEvent(event);
}
在mainActivity的onCreate:
final CornerButton LeftCornerButton = (CornerButton) findViewById(R.id.LeftButton);
LeftCornerButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == (MotionEvent.ACTION_UP)){
LeftCornerButton.setBackgroundResource(R.drawable.cornerleft);
}
else{
LeftCornerButton.setBackgroundResource(R.drawable.cornerleftdark);
}
return true;
}
});
LeftCornerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showEvents(v);
}
});
的触摸检测工作完全和图像翻转效果很好过。 我不能将点击操作放在(event.getAction() == (MotionEvent.ACTION_UP) else
block上,因为它会多次调用intent。
你,先生,真棒。 –