TextView setText似乎干扰了使用MotionEvent拖动Button

时间:2013-03-20 13:17:57

标签: android textview motionevent

我在这里有一些奇怪的行为(无论如何我都很奇怪)。

我有两个按钮,向左/向右拖动也会移动另一个按钮。这个工作正常,直到我使用setText来更新TextView,其中包含它所处的事件的详细信息。如果取消注释下面的tv.setText行,它似乎会导致按钮被设置回原始位置。

如果我做的事显然很傻,有人可以让我知道,我会非常感激。

感谢。

代码在这里:

package com.kk.moveit;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;

public class MoveIt extends Activity {

Button b1,b2;
TextView tv;

int b1X;
int wndwX; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_move_it);

    b1 = (Button)findViewById(R.id.b1);
    b2 = (Button)findViewById(R.id.b2);
    tv = (TextView)findViewById(R.id.tv);

    b1.setOnTouchListener( new OnTouchListener() {

        public boolean onTouch(View view, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    // the view is pressed
                    b1X = (int) event.getX();
                    wndwX = (int) event.getRawX();
   //               tv.setText("ACTION_DOWN");
                    return false;
                //break;                        
                case MotionEvent.ACTION_MOVE:
                // a movement is applied to the view
                    int l0 = view.getLeft();
                    int l = (int) event.getRawX() - b1X;
                    int r = l + view.getWidth();
                    view.layout(l, view.getTop(), r, view.getBottom());
                    move(b2,l-l0);
  //            tv.setText("ACTION_MOVE");
                    return true;
                case MotionEvent.ACTION_UP:
 //                 tv.setText("ACTION_UP");
                    // the view is released
                    return true; // finished with this event
                case (MotionEvent.ACTION_CANCEL):
                    // something happened and the
                    // action was not completed
                    return true; // finished with this event
                case (MotionEvent.ACTION_OUTSIDE):
                    // action happened outside the bounds
                    // of the view
                    return true; // finished with this event    

            }
            return false;
        }
    });
}

private void move(View v, int l) {
    int l0 = v.getLeft();
    v.layout(l+l0, v.getTop(), l+l0+v.getWidth(), v.getBottom());

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_move_it, menu);
    return true;
}

}

和xml在这里:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/b1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button1" />

<Button
    android:id="@+id/b2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button2" />

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    tools:context=".MoveIt" />

1 个答案:

答案 0 :(得分:0)

  

如果下面的tv.setText行被取消注释,它似乎正在造成   要将按钮设置回原始位置。

TextViews上设置文本将触发视图层次结构的新测量和布局。这个新的布局传递将取消您对这些视图位置所做的更改(使用layout()方法),因为LinearLayout具有自己的布局机制,用于放置其子项。

如果你想实现拖动,那么你可以使用SDK拖放框架(如果我没弄错的话,可以使用Honeycomb)或更改当前布局以使用FrameLayoutRelativeLayout root并更改LayoutParams方法中的onTouch视图。