我们的应用程序中,我们使用按钮处理事件来记录数据。
最初,当我同时使用setOnLongClickListener()
和setOnClickListener()
来处理同一个按钮时,它对我们很好用。
这意味着它会基于按钮的单击和长按调用这两个监听器。现在,当我尝试在相同的按钮上使用setOnTouchListener()
,并结合setOnLongClickListener()
和setOnClickListener()
使用时,只有OnTouch事件有效,其余的onclick和onLongclick事件都不起作用。
有人能告诉我为什么会出现这种情况,并且如果可能,用示例解释一下吗?
我使用的代码:
Button btnAdd=new Button(this)
btnAdd.setOnLongClickListener(this);
btnAdd.setOnClickListener(this);
btnAdd.setOnTouchClickListener(this);
public void onClick(View v)
{
//Statements;
}
public void onLongClick(View v)
{
//Statements;
}
public boolean onTouch(View v, MotionEvent e)
{
switch (e.getAction())
{
case MotionEvent.ACTION_DOWN:
{
//store the X value when the user's finger was pressed down
m_downXValue = e.getX();
break;
}
case MotionEvent.ACTION_UP:
{
//Get the X value when the user released his/her finger
float currentX = e.getX();
//MotionEvent x=MotionEvent.obtain((long) m_downXValue, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 1, 1, 1, 1,0, 0, 0, 0, 0);
// going forwards: pushing stuff to the left
if (m_downXValue > currentX && currentX < 0)
{
ViewFlipper vf = (ViewFlipper) findViewById(R.id.flipview);
vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left));
vf.showNext();
}
// going backwards: pushing stuff to the right
if (m_downXValue < currentX && currentX > 100)
{
ViewFlipper vf = (ViewFlipper) findViewById(R.id.flipview);
vf.setAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right));
vf.showPrevious();
}
if (m_downXValue == currentX)
{
onClick(v);
}
break;
}
}
return true;
}