如何检测,当手指滑出的ImageButton? [英] How to detect when finger slides off the ImageButton?

查看:178
本文介绍了如何检测,当手指滑出的ImageButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageButton的,我setColorFilter在ActionDOWN,但如果用户滑动手指离开的ImageButton,它仍然有setColorFilter应用。

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

问:

  • 如何检测,当手指滑落?然后,我可以申请setColorFilter(空);

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

的ImageButton I1 =(的ImageButton)v.findViewById(R.id.imageButton1);          i1.setOnTouchListener(新OnTouchListener(){

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;
        }
    });

推荐答案

您可以使用 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;
    }
});

更新1:

如果 MotionEvent.ACTION_OUTSIDE 不工作,你可以尝试 MotionEvent.ACTION_CANCEL

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

这篇关于如何检测,当手指滑出的ImageButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆