当前位置: 代码迷 >> java >> 当触摸屏幕一次或两次时,如何决定使用哪个参数发送?
  详细解决方案

当触摸屏幕一次或两次时,如何决定使用哪个参数发送?

热度:82   发布时间:2023-07-17 20:26:27.0

我有一个get方法,我向服务器发送两个参数:

private byte[] Get(String urlIn)
    {
        URL url = null;
        String urlStr="http://10.0.0.2:8098/?cmd=start&cmd=stop";
        if (urlIn!=null)
            urlStr=urlIn;

        try
        {
            url = new URL(urlStr);
        } catch (MalformedURLException e)
        {
            e.printStackTrace();
            return null;
        }
        HttpURLConnection urlConnection = null;
        try
        {
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

            byte[] buf=new byte[10*1024];
            int szRead = in.read(buf);

            byte[] bufOut;



            if (szRead==10*1024)
            {
                throw new AndroidRuntimeException("the returned data is bigger than 10*1024.. We don't handle it..");
            }
            else
            {
                bufOut = Arrays.copyOf(buf, szRead);
            }
            return bufOut;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            if (urlConnection!=null)
                urlConnection.disconnect();
        }
    }

在一个touchEvent里面我做了:

@Override
        public boolean onTouchEvent(MotionEvent event)
        {
            float eventX = event.getX();
            float eventY = event.getY();

            float lastdownx = 0;
            float lastdowny = 0;

            switch (event.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    path.moveTo(eventX, eventY);
                    circlePath.addCircle(eventX, eventY, 50, Path.Direction.CW);
                    lastdownx = eventX;
                    lastdowny = eventY;

                    Thread t = new Thread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            byte[] response = Get(null);
                            if (response!=null)
                            {
                                String a = null;
                                try
                                {
                                    a = new String(response,"UTF-8");
                                } catch (UnsupportedEncodingException e)
                                {
                                    e.printStackTrace();
                                }
                                Logger.getLogger("MainActivity(inside thread)").info(a);
                            }
                        }
                    });
                    t.start();

                    return true;
                case MotionEvent.ACTION_MOVE:
                    path.lineTo(eventX, eventY);
                    break;
                case MotionEvent.ACTION_UP:
                    // nothing to do
                    circlePath.reset();

                    break;
                default:
                    return false;
            }
            invalidate();
            return true;
        }

这工作正常,发送两个参数,但现在我想改变它,当我触摸Android屏幕时,它将发送第一个参数:开始。

如果我在屏幕两次触摸后触摸(双触),它将发送第二个参数:停止。

  1. 我如何知道我是否触摸过一次或两次屏幕?

  2. 当我触摸一次它将发送启动参数时,如何触摸它,当触摸两次它将发送第二个参数停止?

  3. 我怎么做才能知道我每次触摸一次还是双倍? 如果我点击一次或者如果我点击两次然后发送正确的参数,就像双击一样?

使用

请参阅底部的设置

  1. 当应用程序确定它不是双击(或三倍或更多)时, onSingleTapConfirmed(..)方法将触发。 onDoubleTap(..)方法将在双击时触发。

2.单击:

@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    // DO STUFF FOR SINGLE TOUCH

    /*
    This return statement says that this method was handled (true)
        or
    This return statement says that this method was not handled (false)
     */
    return true;
}

双击:

@Override
public boolean onDoubleTap(MotionEvent e) {
    // DO STUFF FOR DOUBLE TAP 

    //see above for notes on return statement 
    return true;
}
  1. 见号码(1)

设定:

  • implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener添加到类标题中。
  • 添加字段变量: private GestureDetector gestureDetector;
  • 将以下内容添加到onCreate(..)方法中:

    gestureDetector= new GestureDetectorCompat(this,this); gestureDetector.setOnDoubleTapListener(this);

  • 因为您正在使用界面,所以您需要覆盖您可能使用或不使用的更多方法。 放手去做。 例如:

    @Override public boolean onDown(MotionEvent event) { // not gonna use it, but ok :/ return false; // so it doesn't consume the event }

更多关于

1)您可以使用GestureRecognizer,也可以使用名为timeOfLastTouch的成员变量。 然后在触摸时检查自最后一次点击后的时间。 因此双击。

private long timeOfLastTouch ;


public void OnTouch()
{  
// i have used 300ms as a time frame in which you have to complete your clicks

     if ( timeOfLastTouch + 300 >System.CurrentTimeMiliSeconds())
     {
     // Double touch
     }else { 
     // Single touch
     }
 // reset the variable 
timeOfLastTouch  = System.CurrentTimeMiliSeconds();
}
  相关解决方案