在android中拖动手指时选择多个视图

时间:2012-05-22 14:36:33

标签: java android

我已经动态创建了多个视图,在我的情况下textViews。我给了他们ids。我想要选择i textViews所有slide my finger个id。例如,如果有文字视图说A和B,当我将手指从A滑动到b时,我应该将ids保存在我的阵列中。目前我正在学习触摸。我不知道如何实现它。

最好的问候

  OnTouchListener MyOnTouchListener = new OnTouchListener(){ 
          public boolean onTouch(View v, MotionEvent event) 
          {
              Drawable d = getResources().getDrawable(R.drawable.small_corners);
              switch(event.getAction() & MotionEvent.ACTION_MASK){
                    case MotionEvent.ACTION_DOWN:
                           vibe.vibrate(20);


                               id.add(v.getId());   
                               TextView vv = (TextView) v;
                               val.add(vv.getText().toString()); 
                               vv.setBackgroundDrawable(d);
                               String str = "";



            //A pressed gesture has started, the motion contains the initial starting location.
            return false;
           case MotionEvent.ACTION_POINTER_DOWN:
            //A non-primary pointer has gone down.
            break;
           case MotionEvent.ACTION_MOVE:
            //A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP).
               int check = 0;
               for (int i = 0; i < id.size(); i ++)
               {
                   if (id.get(i) == v.getId())
                       check = 1;
               }
               if (check != 1)
               {
                   id.add(v.getId());
                   vv = (TextView) v;
                   val.add(vv.getText().toString()); 
                   vv.setBackgroundDrawable(d);
               }
            return false;
           case MotionEvent.ACTION_MASK:
                //A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP).

                break;
           case MotionEvent.ACTION_OUTSIDE:
                //A change has happened during a press gesture (between ACTION_DOWN and ACTION_UP).

                break;


           case MotionEvent.ACTION_UP:
            //A pressed gesture has finished.

              break;

           case MotionEvent.ACTION_POINTER_UP:
            //A non-primary pointer has gone up.

            break;
           }

           return false;
          }
         };

2 个答案:

答案 0 :(得分:1)

根据我的理解,试试这个;实施onTouchlistner并为所有TextView设置此监听器,在onTouch()执行此操作:

public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    if(event.getAction() == MotionEvent.ACTION_MOVE){
            //SAVE YOUR VIEWS ID//
            someArray[index] = ((TextView)v).getId()) 
            return true;
     }
     return false;
}

尝试ACTIONS

的不同MotionEvent

答案 1 :(得分:0)

您是否尝试过ACTION_HOVER_ENTER和ACTION_HOVER_EXIT? 它可能会成功。请参阅onGenericMotionEvent的文档中的示例。