如何检测手指滑离ImageButton?

时间:2013-01-19 13:31:34

标签: android touch imagebutton

我在ActionDOWN上有一个Imagebutton和setColorFilter,但是如果用户将手指滑离ImageButton,它仍然会被setColorFilter应用。

问题:

  • 手指滑落时如何检测?然后我可以应用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 个答案:

答案 0 :(得分:1)

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