相关疑难解决方法(0)

获取按钮坐标并检测手指是否在他们身上 - Android

我的Android应用程序活动中有4个按钮.这就是它的样子:

如您所见,屏幕底部的textViews显示x和y坐标.坐标参考相对布局.(见下面给出的代码)

现在,我想要做的是在运行时获取4个按钮的x和y坐标,然后确定我的手指是否在移动时触摸按钮.简单来说,我想用手指按住按钮而不是抬起和触摸按钮.我怎样才能实现它?我想在我的钢琴应用程序中实现它.

我能够在屏幕上获得坐标,并且当我移动手指时它们会改变.
所以,我的问题是如何获得按钮的坐标并检测我的手指是否在它们上方滑动?:
XML

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:id="@+id/relativelayout">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Y Cord : "
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/textView"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="X Cord : "
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp"
    android:id="@+id/textView2"
    android:layout_above="@+id/textView"
    android:layout_alignParentLeft="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textView3"
    android:textColor="#000000"
    android:layout_below="@+id/textView2"
    android:layout_toRightOf="@+id/textView" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textView4"
    android:textColor="#000000"
    android:layout_marginBottom="10dp"
    android:layout_above="@+id/textView"
    android:layout_toRightOf="@+id/textView" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:textColor="#000000"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:textColor="#000000"
    android:id="@+id/button2"
    android:layout_below="@+id/button" …
Run Code Online (Sandbox Code Playgroud)

java android motionevent

6
推荐指数
1
解决办法
5082
查看次数

标签 统计

android ×1

java ×1

motionevent ×1