Android 按下按钮时,上下动作

Android 按下按钮时,上下动作,android,click,Android,Click,我为释放或单击按钮时的检测创建了一个类 现在我需要更改ImageButton中的图像,当按钮被单击时,以及释放时 问题是,如果我按下按钮,MotionEvent.ACTION\u DOWN和MotionEvent.ACTION\u UP同时激活,释放时,我收到ACTION\u DOWN事件 为什么我在单击时收到2个事件,而在释放时仅收到向上事件 button.setOnTouchListener( new View.OnTouchListener() { public boolean

我为释放或单击按钮时的检测创建了一个类

现在我需要更改ImageButton中的图像,当按钮被单击时,以及释放时

问题是,如果我按下按钮,MotionEvent.ACTION\u DOWN和MotionEvent.ACTION\u UP同时激活,释放时,我收到ACTION\u DOWN事件

为什么我在单击时收到2个事件,而在释放时仅收到向上事件

button.setOnTouchListener( new View.OnTouchListener() 
{
    public boolean onTouch(View v, MotionEvent event) 
    {
        switch (event.getAction()) 
        {
            case MotionEvent.ACTION_DOWN: 
            {
                Log.d("log", "onTouch: push");
                button.setImageResource(R.drawable.buttonmason);
            }

            case MotionEvent.ACTION_UP: 
            {
                Log.d("log", "onTouch: release");
                button.setImageResource(R.drawable.buttonmas);
            }
        }

        if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoIncrement )
        {
            mAutoIncrement = false;
        }
        else if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL)  && mAutoDecrement )
        {
            mAutoDecrement = false;
        }

        return false;
    }
});

下面是带break语句的修订代码

button.setOnTouchListener( new View.OnTouchListener() 
{
    public boolean onTouch(View v, MotionEvent event) 
    {
        switch (event.getAction()) 
        {
            case MotionEvent.ACTION_DOWN: 
            {
                Log.d("log", "onTouch: push");
                button.setImageResource(R.drawable.buttonmason);
            }
            break
            case MotionEvent.ACTION_UP: 
            {
                Log.d("log", "onTouch: release");
                button.setImageResource(R.drawable.buttonmas);
            }
        }

        if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoIncrement )
        {
            mAutoIncrement = false;
        }
        else if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL)  && mAutoDecrement )
        {
            mAutoDecrement = false;
        }

        return false;
    }
});

下面是带break语句的修订代码

button.setOnTouchListener( new View.OnTouchListener() 
{
    public boolean onTouch(View v, MotionEvent event) 
    {
        switch (event.getAction()) 
        {
            case MotionEvent.ACTION_DOWN: 
            {
                Log.d("log", "onTouch: push");
                button.setImageResource(R.drawable.buttonmason);
            }
            break
            case MotionEvent.ACTION_UP: 
            {
                Log.d("log", "onTouch: release");
                button.setImageResource(R.drawable.buttonmas);
            }
        }

        if( (event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL) && mAutoIncrement )
        {
            mAutoIncrement = false;
        }
        else if((event.getAction()==MotionEvent.ACTION_UP || event.getAction()==MotionEvent.ACTION_CANCEL)  && mAutoDecrement )
        {
            mAutoDecrement = false;
        }

        return false;
    }
});

你忘了加破口;在开关块内的第一个案例之后。您忘记添加中断;在开关块内的第一个案例之后。