拖动EditText字段,同时允许它可编辑。

时间:2013-10-27 22:13:04

标签: android touch ontouchevent motionevent

我可以使用以下代码在视图中上下拖动EditText字段。毫不奇怪,我无法再编辑EditText字段。我需要这样做,以便当我点击字段键盘出现时,当我拖动它时,拖动代码被实现。我怎样才能区分这两者。我想我需要以某种方式实现GestureDetector。关于如何做到这一点的任何建议?

mSnapText.setOnTouchListener( new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent ev) {

        //float mLastTouchX;
        float mLastTouchY = 0;
        //float mPosX;
        float mPosY = 0;
        RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) v.getLayoutParams();
        switch(MotionEventCompat.getActionMasked(ev)){
        case MotionEvent.ACTION_DOWN: {
            int pointerIndex = MotionEventCompat.getActionIndex(ev);
            //float x = MotionEventCompat.getY(ev, pointerIndex);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            // Remember where we started (for dragging)
            //mLastTouchX = x;
            mLastTouchY = y;
            // Save the ID of this pointer (for dragging)
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            break;
        }   
        case MotionEvent.ACTION_MOVE: {
            int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            //float x = MotionEventCompat.getY(ev, pointerIndex);
            final float y = MotionEventCompat.getY(ev, pointerIndex);
             // Calculate the distance moved
            //float dx = x - mLastTouchX;
            final float dy = y - mLastTouchY;


            //mPosX += dx;
            mPosY += dy;
            float top = rl.topMargin+mPosY;
            if(top<0){
                top=0;
            }
            if(top >600){
                top=600;
            }
            Log.d(TAG,"top"+Float.toString(top));
            rl.topMargin = (int)top;
            v.setLayoutParams(rl);


            // Remember this touch position for the next move event
            //mLastTouchX = x;
            mLastTouchY = y;
            Log.d(TAG,Float.toString(mPosY));
            break;
        }
        case MotionEvent.ACTION_UP: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }          
        case MotionEvent.ACTION_CANCEL: {
            mActivePointerId = INVALID_POINTER_ID;
            break;
        }
        case MotionEvent.ACTION_POINTER_UP: {

            final int pointerIndex = MotionEventCompat.getActionIndex(ev); 
            final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex); 

            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                //mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex); 
                mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex); 
                mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
            }
            break;
        }
        }

        return true;
    }
});

0 个答案:

没有答案