代码之家  ›  专栏  ›  技术社区  ›  Chinmay Dabke

将屏幕坐标从按钮传递给父级

  •  2
  • Chinmay Dabke  · 技术社区  · 10 年前

    我正在为android制作一个钢琴应用程序。作为示例(测试),我的活动中有4个按钮。父级是 Relative Layout 我还有一些文本视图,可以显示手指触摸的屏幕坐标。还有一个textView(“Entered Button”)可以检测您的手指是否在按钮上方
    这是它的样子:

    我使用下面给出的代码实现了这一点。

    Java语言

    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);
    }
    }
    

    XML格式

    <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>
    

    因此,上面给出的代码可以很好地工作,因为当我从空白区域(相对布局)滑动手指时,它可以识别任何按钮。但当我从一个按钮滑动到另一个按钮时,它不起作用。它没有任何坐标,也没有感觉到我手指上的哪个按钮。下图最好地解释了当我从一个按钮滑动到另一个按钮时发生的情况。

    那么,如何在从一个按钮滑动到另一个按钮时获得坐标?

    1 回复  |  直到 10 年前
        1
  •  4
  •   Karioki    10 年前

    主要活动.java

        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 {
    
            MyButton 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 = (MyButton) findViewById(R.id.button1);
                b2 = (MyButton) findViewById(R.id.button2);
                b3 = (MyButton) findViewById(R.id.button3);
                b4 = (MyButton) 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);
                                    b.getBackground().setAlpha(255);
                                }
    
                                if (isPointWithin(x, y, b.getLeft(), b.getRight(), b.getTop(),
                                        b.getBottom())) {
                                    b.getBackground().setState(STATE_PRESSED);
                                    b.getBackground().setAlpha(150);
                                    b.performClick();
    
                                    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);
            }
        }
    

    我的按钮.java

    package com.example.touch;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.widget.Button;
    
    public class MyButton extends Button {
    
        public MyButton(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            // TODO Auto-generated constructor stub
        }
    
        public MyButton(Context context, AttributeSet attrs) {
            super(context, attrs);
            // TODO Auto-generated constructor stub
        }
    
        public MyButton(Context context) {
            super(context);
            // // TODO Auto-generated constructor stub
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub
            // return super.onTouchEvent(event);
            return false;
        }
    
    }
    

    布局:

    <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" />
    
        <com.example.touch.MyButton
            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" />
    
        <com.example.touch.MyButton
            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" />
    
        <com.example.touch.MyButton
            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" />
    
        <com.example.touch.MyButton
            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>