2013-03-20 59 views
2

我在这里有一些奇怪的行为(反正我看起来很奇怪)。TextView setText出现干扰拖动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" /> 

回答

0

如果下面的tv.setText线是未加注释,它似乎导致 按钮被设置回到原来的位置。

设置这些文本TextViews将触发视图层次结构的新测量和布局。此新布局通行证将取消您对这些查看位置所做的更改(使用layout()方法),因为LinearLayout拥有自己的布局机制,用于放置子级。

如果你想实现拖动,那么你可以使用SDK拖放框架(可带蜂窝,如果我没有记错的话)或改变当前布局使用FrameLayoutRelativeLayout根和改变的意见LayoutParamsonTouch方法中。

+0

非常好,谢谢。设置TextView的宽度以匹配父项目。 – user2190908 2013-03-20 14:24:13