1

我有一个 Imagebutton,我在 ActionDOWN 上设置了 ColorFilter,但是如果用户将手指从 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;
            }
        });
    
4

1 回答 1

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

于 2013-01-19T13:37:42.187 回答