I would like the user of my Android application to be able to move the contents of one TextView (A) to another TextView (B). They should be able to do this by dragging from A and releasing over B. I've tried to achieve this using the onTouch event, particularly ACTION_DOWN and ACTION_UP. I thought this would be straight forwa开发者_JS百科rd but I've encountered a few problems.
Attempt One
I first tried to capture the View passed to each call to the event handler. This is fine for ACTION_DOWN as A is passed in as the View. However, on the ACTION_UP event, A is also passed as the View rather than B.
Attempt Two
I capture the raw coordinates of the ACTION_UP event (getRawX(), regRawY()) and then compare these coordinates to the Left, Right, Top & Bottom of the Views onto which the user can “drop”. Unfortunately, the View coordinates don't appear to relate to the event coordinates.
Question
How do a implement this simple drag-and-drop or what mistake am I making in my two attempts.
Listener code (attempt two)
OnTouchListener PlayerListener = new OnTouchListener () {
public boolean onTouch(View v, MotionEvent me) {
boolean mProcessed = false;
int mActionX = 0;
int mActionY = 0;
int mAction = me.getAction();
/* Determine gesture */
switch (mAction) {
case MotionEvent.ACTION_DOWN:
mProcessed = theTeams.touchPlayer(v,mAction);
break;
case MotionEvent.ACTION_UP:
mActionX = (int) me.getX();
mActionY = (int) me.getY();
mProcessed = theTeams.endTouch(v,mActionX,mActionY);
break;
Handler code (attempt two)
public boolean endTouch(View v, int dropX, int dropY) {
int mLeft = 0;
int mRight = 0;
int mTop = 0;
int mBottom = 0;
boolean mFound = false;
View mView = null;
/* Find control at that position */
mView = (TextView) findViewById(R.id.lCourt1ok);
mLeft = mView.getLeft();
mRight = mView.getRight();
mTop = mView.getTop();
mBottom = mView.getBottom();
if ((dropX >= mLeft) && (dropX <= mRight )
&& (dropY <= mBottom) && (dropY >= mTop))
mFound = true;
Postscript
Well, things seem to be broadly OK with Attempt Two when I subtract 50 from the RawY coordinate. Any suggestions why?
Well, things seem to be broadly OK with Attempt Two when I subtract 50 from the RawY coordinate. Any suggestions why?
I'm guessing that you have a status bar and a title bar. I think those are each about 25 pixels high, so with both, the elements on your screen would be 50 px off from what you would expect.
精彩评论