处理触摸事件

时间:2016-01-22 10:48:00

标签: 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"
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>

的活动:

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

问题在于&#34; test_button_1&#34;是可点击的(OnTouchListener和OnClickListener正常工作),但&#34; test_button_2&#34; ISN&#39;吨。甚至OnTouchListener for&#34; parent_test_button_2&#34;不起作用。

所以我有两个问题:   - 如何制作&#34; test_button_2&#34;点击;   - 为什么android进程会以这种方式触及(我将非常感谢伟大的描述或链接到文章)。

编辑:根据Rajen Raiyarela评论:OnTouchListener添加到&#34; parent_test_button_2&#34;永远不会被称为。

0 个答案:

没有答案