的onTouchEvent的onClick

的onTouchEvent的onClick

本文介绍了的onTouchEvent的onClick onLongClick电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

所以最初当我使用 setOnLongClickListener() setOnClickListener()与同一个按钮,它做工精细对我们来说。

这意味着它会调用这两个按钮的点击和LongClick此听众基地。现在,当我试图用 setOnTouchListener()加上与 setOnLongClickListener()和 setOnClickListener()则仅OnTouch事件是工作,休息的onclick和onLongclick事件不工作。

谁能告诉我,为什么出现这种情况,如果可能的话解释我用一个例子。

在code我用的是..

 按钮btnAdd =新的按钮(这一点)

btnAdd.setOnLongClickListener(本);

btnAdd.setOnClickListener(本);

btnAdd.setOnTouchClickListener(本);

公共无效的onClick(视图v)
{
    //语句;
}

公共无效onLongClick(视图v)
{
    //语句;
}

公共布尔onTouch(视图V,MotionEvent E)
{
    开关(e.getAction())
    {
        案例MotionEvent.ACTION_DOWN:
        {
            //存储X值时,用户的手指被pressed向下
            m_downXValue = e.getX();
            打破;
        }

        案例MotionEvent.ACTION_UP:
        {
            //获取X值,当用户发布了他/她的手指
            浮currentX = e.getX();
            // MotionEvent X = MotionEvent.obtain((长)m_downXValue,SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,1,1,1,1,0,0,0,0,0);

            //将前锋:推动东西向左
            如果(m_downXValue> currentX&安培;&安培; currentX℃下)
            {
                ViewFlipper VF =(ViewFlipper)findViewById(R.id.flipview);
                vf.setInAnimation(AnimationUtils.loadAnimation(这一点,R.anim.slide_left));


                vf.showNext();

            }

            //退步:推东西的权利
            如果(m_downXValue&其中; currentX&安培;&安培; currentX→100)
            {
                ViewFlipper VF =(ViewFlipper)findViewById(R.id.flipview);
                vf.setAnimation(AnimationUtils.loadAnimation(这一点,R.anim.slide_right));


                vf.show previous();

            }

            如果(m_downXValue == currentX)
            {
                的onClick(五);
            }

            打破;
        }
    }
    返回true;
}
 

解决方案

据美国商务部处理UI事件的,

和以上所有关于onTouch:

事实上,根据不同的情况下,你必须回到正确的价值。

In our application we handle the events in button to record data.

So initially when i use setOnLongClickListener() and setOnClickListener() together with the same button, it work fine for us.

That means it will call both this listener base on the click and LongClick of the button. Now when i tried to use setOnTouchListener() with the same button together with setOnLongClickListener() and setOnClickListener() then only OnTouch event is working, rest onclick and onLongclick event doesnt work.

Can anyone tell me why this happen and if possible explain me with an example.

The code i use is..

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;
}
解决方案

According to the doc Handling UI Events,

And above all about onTouch:

Indeed, depending on the event, you have to return the right value.

这篇关于的onTouchEvent的onClick onLongClick电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:19