Android 处理触摸事件

Android 处理触摸事件,android,touch,touch-event,Android,Touch,Touch Event,下面是一个处理“奇怪”触摸事件的示例 我创建了一个示例项目来清楚地解释这个问题 我的布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

下面是一个处理“奇怪”触摸事件的示例

我创建了一个示例项目来清楚地解释这个问题

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<View
    android:id="@+id/red_square"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_centerInParent="true"
    android:background="#F00" />

<Button
    android:id="@+id/test_button_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:text="Test button #1" />

<FrameLayout
    android:id="@+id/parent_test_button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/test_button_1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="48dp"
    android:background="#0F0"
    android:clickable="true"
    android:padding="16dp">

    <Button
        android:id="@+id/test_button_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test button #2" />
</FrameLayout>

<View
    android:id="@+id/touch_layer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true" />

</RelativeLayout>
问题是“测试按钮1”是可点击的(OnTouchListener和OnClickListener工作正常),但“测试按钮2”不是。即使是“家长测试按钮2”的OnTouchListener也不起作用

所以我有两个问题: -如何使“测试按钮2”可点击;
-为什么android进程会以这种方式触及(我将非常感谢对本文的精彩描述或链接)


编辑:根据Rajen Raiyarela的评论:添加到“家长测试按钮2”的OnTouchListener从未被调用。

为什么android进程会以这种方式进行操作。。。以哪种方式?另外,由于您有一个按钮2的parentOfButton2的侦听器(android:id=@+id/parent_test_button2),所以您的事件都没有到达按钮2。尝试删除从未调用“parent_test_button_2”的.OnTouchListener。这在我心目中是相当“奇怪”的。为什么android进程会以这种方式接触。。。以哪种方式?另外,由于您有一个按钮2的parentOfButton2的侦听器(android:id=@+id/parent_test_button2),所以您的事件都没有到达按钮2。尝试删除从未调用“parent_test_button_2”的.OnTouchListener。这是我心中相当“奇怪”的想法。
public class MainActivity extends AppCompatActivity {

private View mRedSquare;


private GestureDetector mDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            mRedSquare.setTranslationX(mRedSquare.getTranslationX() - distanceX);
            mRedSquare.setTranslationY(mRedSquare.getTranslationY() - distanceY);
            return true;
        }
    });

    mRedSquare = findViewById(R.id.red_square);

    View touchLayer = findViewById(R.id.touch_layer);
    touchLayer.setOnTouchListener(mTouchLayerListener);

    View button1 = findViewById(R.id.test_button_1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Button #1 click!", Toast.LENGTH_SHORT).show();
        }
    });
    button1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("TOUCH_DEBUG", "bt1: " + event.toString());
            return false;
        }
    });

    View button2 = findViewById(R.id.test_button_2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "Button #2 click!", Toast.LENGTH_SHORT).show();
        }
    });
    button2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("TOUCH_DEBUG", "bt2: " + event.toString());
            return false;
        }
    });

    FrameLayout parentOfButton2 = (FrameLayout) findViewById(R.id.parent_test_button2);
    parentOfButton2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("TOUCH_DEBUG", "bt2 parent: " + event.toString());
            return false;
        }
    });
}

private View.OnTouchListener mTouchLayerListener = new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return mDetector.onTouchEvent(event);
    }
};
}