【问题标题】:TextView setText appearing to interfere with dragging Button with MotionEventTextView setText 似乎会干扰使用 MotionEvent 拖动 Button
【发布时间】:2013-03-20 13:17:56
【问题描述】:

我在这里有一些奇怪的行为(反正我觉得很奇怪)。

我有两个按钮,向左/向右拖动一个也会移动另一个。这可以正常工作,直到我使用 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" />

【问题讨论】:

    标签: android textview motionevent


    【解决方案1】:

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

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

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

    【讨论】:

    • 太好了,谢谢。设置 TextView 的宽度以匹配父级也可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 2012-06-09
    相关资源
    最近更新 更多