How to detect when finger slides off the ImageButton?

梦想与她 提交于 2019-12-08 05:43:58

问题


I have an Imagebutton and I setColorFilter on ActionDOWN, but if user Slide the finger off the ImageButton, it remains with setColorFilter applied.

Question:

  • How to detect when finger slides off? Then I can apply setColorFilter(null);

    ImageButton i1 = (ImageButton)v.findViewById(R.id.imageButton1); i1.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                ImageButton button = (ImageButton)v;
                String principal = "principal";
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    button.setColorFilter(0x8066bbdd);
                    return true;
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    button.setColorFilter(null);
                    Intent i = new Intent(getActivity(), SubView.class);
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    i.putExtra("query", principal);
                    startActivity(i);
                    return true;
                }
                return false;
            }
        });
    

回答1:


You can use MotionEvent.ACTION_OUTSIDE

ImageButton i1 = (ImageButton)v.findViewById(R.id.imageButton1); i1.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        ImageButton button = (ImageButton)v;
        String principal = "principal";
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            button.setColorFilter(0x8066bbdd);
            return true;
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            button.setColorFilter(null);
            Intent i = new Intent(getActivity(), SubView.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.putExtra("query", principal);
            startActivity(i);
            return true;
        } else if(event.getAction() == MotionEvent.ACTION_OUTSIDE)
        {

            // Do some stuffs here
        }

        return false;
    }
});

UPDATE 1:

If MotionEvent.ACTION_OUTSIDE Doesn't worked you can try MotionEvent.ACTION_CANCEL



来源:https://stackoverflow.com/questions/14414873/how-to-detect-when-finger-slides-off-the-imagebutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!