Android - 实现 TextView 之间的拖放

发布于 2024-10-08 20:40:22 字数 1724 浏览 0 评论 0原文

我希望 Android 应用程序的用户能够将一个 TextView (A) 的内容移动到另一个 TextView (B)。他们应该能够通过从 A 拖动并在 B 上释放来完成此操作。我尝试使用 onTouch 事件(特别是 ACTION_DOWN 和 ACTION_UP)来实现此目的。我认为这很简单,但我遇到了一些问题。

尝试一

我首先尝试捕获传递给事件处理程序的每次调用的视图。这对于 ACTION_DOWN 来说很好,因为 A 作为视图传入。但是,在 ACTION_UP 事件上,A 也作为视图而不是 B 传递。

尝试二

我捕获 ACTION_UP 事件的原始坐标(getRawX()、regRawY()),然后将这些坐标与左、右、上进行比较&用户可以“放置”到的视图底部。不幸的是,视图坐标似乎与事件坐标无关。

问题

如何实现这个简单的拖放操作或者我在两次尝试中犯了什么错误。

侦听器代码(尝试二)

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;

处理程序代码(尝试二)

  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;

后记

嗯,当我从 RawY 坐标中减去 50 时,尝试二的情况似乎基本正常。有什么建议吗?

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 forward 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-10-15 20:40:22

嗯,当我从 RawY 坐标中减去 50 时,尝试二的情况似乎基本没问题。有什么建议吗?

我猜你有一个状态栏和一个标题栏。我认为这些元素的高度约为 25 像素,因此,如果使用这两种元素,屏幕上的元素将与您期望的元素相差 50 像素。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文