android - onTouchEvent onClick onLongClick 调用

标签 android touch-event onlongclicklistener

在我们的应用程序中,我们处理按钮中的事件以记录数据。

所以最初当我将 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;
}

最佳答案

根据文档 Handling UI Events ,

onLongClick() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners.

最重要的是关于onTouch:

onTouch() - This returns a boolean to indicate whether your listener consumes this event. The important thing is that this event can have multiple actions that follow each other. So, if you return false when the down action event is received, you indicate that you have not consumed the event and are also not interested in subsequent actions from this event. Thus, you will not be called for any other actions within the event, such as a finger gesture, or the eventual up action event.

确实,根据事件的不同,您必须返回正确的值。

关于android - onTouchEvent onClick onLongClick 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7333608/

相关文章:

尝试上传到生产环境时,Android 签名的 APK 显示为未签名的 APK

android - Android 中如何知道 View 处于触摸状态

android - 整个 Activity 上的 OnTouchListener

Android:如何将 'view' 转换为 'textview'?

android - 在 Fedora 中编译并在 Android 上运行

android - Android R 类中的 attr 是什么?

android - Eclipse 停止识别我的 Android 设备

javascript - 在 touchstart 上获取元素?

android - 使用子采样比例 ImageView 在触摸位置添加标记针

android - onLongClick 只应在条件为真时发生