将手指滑入按钮android

问题描述 投票:2回答:1

如果有人将手指滑入或按下按钮,该按钮是否会被推动?

我正在寻找的一个例子是键盘应用程序。您将手指滑过所有按键以播放声音的位置。

java android touch multi-touch
1个回答
0
投票

您可以像这样使用OnTouchListener:

    boolean firstTime = true;
    OnTouchListener testTouchListener = new OnTouchListener(){
        public boolean onTouch(View v, MotionEvent me){
            Rect r = new Rect();
            secondButton.getDrawingRect(r);
            if(r.contains((int)me.getX(),(int)me.getY())){
                //Log.i(myTag, "Moved to button 2");
                if(firstTime == true){
                    firstTime = false;
                    secondButton.performClick();
                }
            }
            if(me.getAction() == MotionEvent.ACTION_UP){
                //When we lift finger reset the firstTime flag
                firstTime = true;
            }
            return false;

        }
    };
    firstButton.setOnTouchListener(testTouchListener);

使用这种方法虽然你会得到大量的触摸事件,因为onTouch()会被MotionEvent.ACTION_MOVE大量调用。因此,您必须保留一个布尔值,告诉您它是否是第一次进行onTouch()调用。然后你可以在onTouch()中为MotionEvent.ACTION_UP重置该布尔值,以便下次再次使用它。但是,如果您尝试的不仅仅是2个按钮,这可能会变得复杂。我认为你必须为它们中的每一个单独保留一个布尔值(或者可能是一系列布尔值来保存它们)。并且你需要为每个按钮添加一个额外的if(r.contains(x,y)语句。这应该让你开始走上正确的道路。

© www.soinside.com 2019 - 2024. All rights reserved.