1

我创建了一个编辑文本,它在触摸时移动,键盘在双击或长按时打开。它工作得很好,但我面临两个问题:

  1. EditText 可以从文本的最后或从文本的开头进行编辑。我无法选择光标的位置。

  2. 编辑文本从左角而不是中心拖放。

触摸监听器的代码是:

@Override
public boolean onTouchEvent(MotionEvent event)
{

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) v.getLayoutParams();
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            setEnabled(true);
            setCursorVisible(true);

            break;

        case MotionEvent.ACTION_UP:
            Log.d("up", "ACTION_UP");
            params.height = LayoutParams.WRAP_CONTENT;
            params.width = LayoutParams.WRAP_CONTENT;
            params.topMargin = (int)(event.getRawY() - (v.getHeight()/2));
            params.leftMargin = (int)(event.getRawX() - (v.getWidth()/2));
            txt_metaData.Xmargin= params.topMargin;
            txt_metaData.YMargin= params.leftMargin;
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d("moving", "ACTION_MOVE");

            params.topMargin = (int)(event.getRawY() - (v.getHeight()/2));
            params.leftMargin = (int)(event.getRawX() - (v.getWidth()/2));
            v.setLayoutParams(params);
            break;
    }
    return gestureScanner.onTouchEvent(event);
}

@Override
public boolean onDown(MotionEvent e)
{   
    return true;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
    return true;
}
@Override
public void onLongPress(MotionEvent e)
{
    Log.i("double tap called", "yes");
    ((InputMethodManager)_mContext.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    setEnabled(true);
    requestFocus();
    requestFocusFromTouch();
}
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
        {
            return true;
        }

        @Override
        public void onShowPress(MotionEvent e)
        {


        }    
        @Override
        public boolean onSingleTapUp(MotionEvent e)    
        {
            return true;
        }
        @Override
        public boolean onDoubleTap(MotionEvent e) 
        {
            Log.i("double tap called", "No");
             ((InputMethodManager)_mContext.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
                setEnabled(true);
                requestFocus();
                requestFocusFromTouch();
             return true;
        }
        @Override
        public boolean onDoubleTapEvent(MotionEvent e) 
        {
             return true;
        }
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) 
        {
            bringToFront();
            return true;
        }
    }
4

0 回答 0