安卓按钮拖放

5

我是一名 Android 开发者,目前正在使用一个按钮。现在我想要实现拖放这个按钮。

这是我的 main.xml 文件

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ll_first"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
>

 <Button  
   android:id="@+id/btn"
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
    android:text="drag me"
  />
 </LinearLayout>

这是我用于在鼠标拖动时移动按钮的代码:
public class DragdropActivity extends Activity implements OnTouchListener {

private final static int START_DRAGGING = 0;
private final static int STOP_DRAGGING = 1;

private Button btn;

private int status;

private ImageView image;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn = (Button) findViewById(R.id.btn);
    btn.setDrawingCacheEnabled(true);
    btn.setOnTouchListener(this);

}

@Override
public boolean onTouch(View view, MotionEvent me) {
    if (me.getAction() == MotionEvent.ACTION_DOWN) {
        status = START_DRAGGING;


    }
    if (me.getAction() == MotionEvent.ACTION_UP) {
        status = STOP_DRAGGING;
        Log.i("Drag", "Stopped Dragging");
    } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
        if (status == START_DRAGGING) {
            System.out.println("Dragging");
            Log.v("***Drag and drop **",
                    "me.getRawX and Y = " + me.getRawX() + " "
                            + me.getRawY());
            Log.v("***Drag and drop **",
                    "image position = " + image.getLeft() + " "
                            + image.getRight());
     btn.setPadding((int) me.getRawX(), (int) me.getRawY(), 0,0); //this is not working fine. 


            btn.invalidate();
        }
    }
    return false;
}

我认为btn.setPadding()没有正确地工作,请建议我应该怎么做,以便按钮可以轻松移动到鼠标或手势触摸的位置?

3个回答

5
我是这样解决我的问题的。
    @Override
     public boolean onTouch(View view, MotionEvent me) {
    if (me.getAction() == MotionEvent.ACTION_DOWN) {
        status = START_DRAGGING;


    }
    if (me.getAction() == MotionEvent.ACTION_UP) {
        status = STOP_DRAGGING;
        Log.i("Drag", "Stopped Dragging");
    } else if (me.getAction() == MotionEvent.ACTION_MOVE) {
        if (status == START_DRAGGING) {
            System.out.println("Dragging");

            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    50, 50);
            layoutParams.setMargins((int) me.getRawX() - 25,
                    (int) me.getRawY() - 50, 0, 0);
            layout.removeView(btn);
            layout.addView(btn, layoutParams);

            btn.invalidate();
        }
    }
    return false;
}

现在它正常工作。

我也尝试了这段代码,一开始它并没有起作用,但是在将返回值更改为“true”之后它就正常工作了。谢谢! - user2235615

2
你需要获取按钮的布局参数,设置left、right、top和bottom,然后将这些布局参数设置回按钮。

2
尝试从 onTouch() 方法中返回true。有可能会起作用。在我的一个应用程序中,由于返回false没有按照我的触摸移动来移动视图,但是返回true语句解决了这个问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接