0

我正在努力完成以下事情

假设我有 5-6 个TextViewsS N A K E

现在,我想按StextView,然后将手指滑过去N以选择它,然后移到 E。我想这样做,以便我可以SNAKE在我的字符串或字符序列等中获得“”

如果有任何想法,请分享我。我不知道如何在onTouch这里使用。另外,我是textViews动态添加的,所以我也在动态设置他们的 ID 最好的问候

4

1 回答 1

0

我从来没有这样做过,但这里有一个想法。

假设您的所有文本视图都处于某种布局中,您可以禁用文本视图的可聚焦性并在您的底层容器上创建一个 onTouch 侦听器。

开始跟踪鼠标按下事件

@Override public boolean onTouch(View v, MotionEvent event) { 
    if(event.getAction == MotionEvent.ACTION_DOWN)
    {
         captureInput = true;
    }
}

*在您的 ACTION_MOVE 事件中,检查当前位置是否在文本视图之上,如果是,则捕获该值*

@Override public boolean onTouch(View v, MotionEvent event) { 
    int action = event.getAction();

    if(action == MotionEvent.ACTION_DOWN)
    {
         captureInput = true;
    }
    else if(action == MotionEvent.ACTION_MOVE
    {
        //get x/y of your finger
         int X = (int)event.getX();   
         int Y = (int)event.getY();

        //Somehow check if the x,y are overlapping with one of your textivews
        // If so, add that textview's text to your list
        // Below is pseudo code
        /* for(TextView curTV: allTextViews)
           {
               if(isOverlapping(X,Y))
               {
                   listOfSwipedCharacters.add(curTV.getText());
               }
           }
     }


}

最后,当用户松开手指时,停止跟踪并对单词列表做一些事情

@Override public boolean onTouch(View v, MotionEvent event) { 
    int action = event.getAction();

    if(action == MotionEvent.ACTION_DOWN)
    {
         captureInput = true;
    }
    else if(action == MotionEvent.ACTION_MOVE
    {
        //get x/y of your finger
         int X = (int)event.getX();   
         int Y = (int)event.getY();

        //Somehow check which textview the X,Y coords are overlapping
        // Below is pseudo code
        /* for(TextView curTV: allTextViews)
           {
               if(isOverlapping(X,Y))
               {
                   listOfSwipedCharacters += (curTV.getText());
               }
           }*/
     }
     else if(action == MotionEvent.ACTION_UP)
     {
         captureInput = false;
         //Do whatever you want with the string of information that you've captured
     }

}

再一次,我不知道这是否可行,这只是对一种方法的猜测。我怀疑通过每个 textview 循环检查重叠的性能会非常糟糕,因此可能有比运行 for 循环更有效的方法。

由于您知道在滑动时不能跳过字母,因此您可以在添加新字母时创建所有可能的滑动位置(周围字母)的简短列表。然后在您的鼠标事件中,检查鼠标移动事件是否击中了这些字母。例如,在一个标准的单词搜索游戏中,一旦你选择了一个字母,你就只有 9 个其他可能的位置可以滑动(周围的字母),所以你应该只在捕获这 9 个位置时检查这 9 个位置是否被击中。鼠标移动事件。

Edit: I suppose you could use the solution above if you attached an onTouch listener to each and every textview. The same premise in regards to touch would apply: Down = start capture, move = capture text, up = stop capture. It would save you from having to identify which textview is being hovered as the textview that fires the onTouch event will be the one that the user is swiping across.

于 2012-05-01T16:20:24.520 回答