Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-如何简单地拖放按钮?_Android_Button_Drag And Drop_Draggable - Fatal编程技术网

Android-如何简单地拖放按钮?

Android-如何简单地拖放按钮?,android,button,drag-and-drop,draggable,Android,Button,Drag And Drop,Draggable,我在这里找到了一个初学者友好的教程: 以下是我的示例中的xml代码: <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/absLayout" android:layout_width="match_parent" android:layout_height="mat

我在这里找到了一个初学者友好的教程:

以下是我的示例中的xml代码:

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/absLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

    <Button
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Countries" />

</AbsoluteLayout>
基本上这里发生了什么,我需要先按一下myButton,将布尔值转换为true。然后,我可以通过触摸布局上的任何地方来拖动我的按钮。我怎么能简单地拖动按钮而不需要先触摸按钮

我尝试在myButton touch listener上设置LayoutParams。它会导致拖动进度闪烁

此外,AbsoluteLayout始终标记为已弃用。“弃用”是什么意思?这是否意味着过时,因此无法使用?或可用,但可能有更好的解决方案

myButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        touched = true;
        return false;
    }
});
上面的部分负责触摸,使您的按钮可以拖动。现在删除这部分代码,并将声明更改为
boolean=true


现在,您应该能够在不触摸按钮的情况下进行拖动。

查看类似的问题和我的答案[此处][1][1]:最好先防止按钮被点击,但是我仍然需要点击absLayout上的任何地方,而不是按钮本身。你能告诉我为什么要使用绝对布局吗?你可以使用线性或相对布局,它比绝对布局好得多(它已被弃用,不再使用)。
myButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        touched = true;
        return false;
    }
});