获取按钮坐标并检测手指是否在其上方 - Android

7
我在我的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"
    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/button3"
    android:layout_below="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" />

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

</RelativeLayout>

Java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView xcordview = (TextView) findViewById(R.id.textView4);
    final TextView ycordview = (TextView) findViewById(R.id.textView3);
    RelativeLayout touchview = (RelativeLayout) findViewById(R.id.relativelayout);
    touchview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            xcordview.setText(String.valueOf(event.getX()));
            ycordview.setText(String.valueOf(event.getY()));
            return true;
        }
    });

  }
}

非常感谢大家!

更新:

public class MainActivity extends Activity {
RelativeLayout touchview;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final TextView xcordview = (TextView) findViewById(R.id.textView4);
    final TextView ycordview = (TextView) findViewById(R.id.textView3);
    touchview = (RelativeLayout) findViewById(R.id.relativelayout);
    touchview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            xcordview.setText(String.valueOf(event.getX()));
            ycordview.setText(String.valueOf(event.getY()));
            for(int i = 0; i < touchview.getChildCount(); i++){
                if(checkInterSection(touchview.getChildAt(i), event.getRawX(), event.getRawY())){
                    Button button = (Button) findViewById(R.id.button);
                    button.setBackgroundColor(Color.BLUE);
                    break;
                }
            }
            return true;
        }
    });

}
private boolean checkInterSection(View view, float rawX, float rawY) {
    int[] location = new int[2];
    view.getLocationOnScreen(location);
    int x = location[0];
    int y = location[1];
    int width = view.getWidth();
    int height = view.getHeight();
    //Check the intersection of point with rectangle achieved
    return (!(rawX < x || rawY > x + width || rawY < y || rawY > y + height));
}

}
4个回答

6
package com.example.touch;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button b1, b2, b3, b4;

    int b1x1, b1x2, b1y1, b1y2;

    private TextView xcordview;
    private TextView ycordview;
    private TextView buttonIndicator;
    private RelativeLayout touchview;
    private static int defaultStates[];
    private Button mLastButton;
    private final static int[] STATE_PRESSED = {
            android.R.attr.state_pressed,
            android.R.attr.state_focused  
                    | android.R.attr.state_enabled };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xcordview = (TextView) findViewById(R.id.textView4);
        ycordview = (TextView) findViewById(R.id.textView3);
        buttonIndicator = (TextView) findViewById(R.id.button_indicator);
        touchview = (RelativeLayout) findViewById(R.id.relativelayout);

        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        defaultStates = b1.getBackground().getState();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        touchview.setOnTouchListener(new View.OnTouchListener() {

            private boolean isInside = false;

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                int x = (int) event.getX();
                int y = (int) event.getY();

                xcordview.setText(String.valueOf(x));
                ycordview.setText(String.valueOf(y));

                for (int i = 0; i < touchview.getChildCount(); i++) {
                    View current = touchview.getChildAt(i);
                    if (current instanceof Button) {
                        Button b = (Button) current;

                        if (!isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                b.getBottom())) {
                            b.getBackground().setState(defaultStates);
                        }

                        if (isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                b.getBottom())) {
                            b.getBackground().setState(STATE_PRESSED);
                            if (b != mLastButton) {
                                mLastButton = b;
                                buttonIndicator.setText(mLastButton.getText());
                            }
                        }

                    }
                }
                return true;
            }

        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
    }

    static boolean isPointWithin(int x, int y, int x1, int x2, int y1, int y2) {
        return (x <= x2 && x >= x1 && y <= y2 && y >= y1);
    }
}

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

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

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

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

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

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

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1"
        android:text="B2"
        android:textColor="#000000" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button2"
        android:text="B3"
        android:textColor="#000000" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button3"
        android:text="B4"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/button_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/textView4"
        android:layout_marginRight="33dp"
        android:text="No one"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button_indicator"
        android:layout_alignBottom="@+id/button_indicator"
        android:layout_marginRight="29dp"
        android:layout_toLeftOf="@+id/button_indicator"
        android:text="Entered: "
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

谢谢!但是什么是简单的数学?您能否提供代码让我更好地理解? - Chinmay Dabke
例如对于第一个按钮,y 应该在 getLeft() 和 getRight() 之间。 - Karioki
请查看我上面的回答。 - Karioki
你能帮我吗?你的代码有一个小错误。当我把手指从白色空间移到按钮上时,它可以完美地识别按钮。但是当我把手指放在按钮上,然后移动到另一个按钮时,它就不起作用了。拜托拜托,请帮帮我!我真的快完成我的应用程序了! - Chinmay Dabke
让我们在聊天中继续这个讨论 - Karioki

4
private boolean checkInterSection(View view, int rawX, int raxY) {
    int[] location = new int[2];
    view.getLocationOnScreen(location);
    int x = location[0];
    int y = location[1];
    int width = view.getWidth();
    int height = view.getHeight();
    //Check the intersection of point with rectangle achieved 
    return (!(rawX < x || rawY > x + width || rawY < y || rawY > y + height)); 
}

for(int i = 0; i < touchview.getChildCount(); i++){
    if(checkInterSection(touchview.getChildAt(i), event.getRawX(), event.getRawY())){
        if(checkInterSection(touchview.getChildAt(i), event.getRawX(), event.getRawY())){
            ((Button)touchview.getChildAt(i)).setBackgroundColor(Color.BLUE);// Type casting may not be required 
        }else{
            ((Button)touchview.getChildAt(i)).setBackgroundColor(Color.WHITE);
        }
        break;
    }
}

0

对于 OnTouchListener,getX() 和 getY() 方法会返回相对于相应视图的坐标。

如果您更喜欢屏幕坐标,可以使用以下方法之一

  • 重写活动的 onTouchEvent 方法并删除按钮的 OnTouchListener,这样 MotionEvent 将报告屏幕坐标而不是按钮坐标

       @Override 
       public boolean onTouchEvent (MotionEvent event) {
           xcordview.setText(String.valueOf(event.getX()));
           ycordview.setText(String.valueOf(event.getY()));
           return true;
       }
    
  • 在按钮的 OnTouchListener 中使用 getTop() 和 getLeft():

      touchview.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            xcordview.setText(String.valueOf(event.getX()+v.getLeft()));
            ycordview.setText(String.valueOf(event.getY()+v.getTop()));
            return true;
        }
    });
    
你可以考虑使用GestureDetector来检测滑动操作,但这并不是必需的。

请您提供两种解决方案的代码,这样我可以更好地理解它。 - Chinmay Dabke
我猜可能有一些错误!你提供的第一个解决方案和我的问题中的代码是一样的! - Chinmay Dabke

0

的解决方案很好。为了使它在我们从其中一个按钮内部开始移动时也能正常工作,只需在xml中为每个按钮添加

android:clickable="false"

即可。


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