开发者

Moving the Edit Text

I have an EditText which is laid out in a Framelayout. I am trying to allow the EditText to be movable around the screen when selected.

Hence, whenever the user selects the mode which allows moving of items, I will set the edit text enabled(false), this works by removing the possibility of havin开发者_如何学运维g edit text to get focus and show up the blinking cursor or the softkeyboard. But at the same time, I cannot receive any touch input when I attempt to move it.

Is there any solution of which I will be able to move the edit text and not having the edit text to receive focus?

I tried setting the focusable or focusableInTouchMode to false, but it doesn't work either.


I've done this by two settings. First you need to have your wrapper structure (LinearLayout) handle the move events. Hook this to your wrapper's Touch event:

void FrameTouch (object sender, View.TouchEventArgs e)
{

    if (e.Event.Action == MotionEventActions.Move) {
        int placeX = (int)e.Event.GetX () - v.Width / 2;
        int placeY = (int)e.Event.GetY () - v.Height / 2;
        v.Layout (placeX, placeY, placeX+v.Width, placeY+v.Height);
    }

}

Then you need to override your EditText's onTouch event to make sure it doesn't swallow the touch events while it is disabled:

public override bool OnTouchEvent (MotionEvent e)
{
    if (!Enabled) 
        return false;

    base.OnTouchEvent (e);
    return true;
}


Try wrapping the EditText in it's own layout (like a LinearLayout), that way you can have the touch events react from the layout instead of the EditText.


I think using a FrameLayout may not be the right choice:

All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot specify a different location for a child view. (from docs)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜