简体   繁体   English

按钮后面的LinearLayout上的OnTouchListener

[英]OnTouchListener on LinearLayout that is behind buttons

I am working on an Android application using C# in Xamarin. 我正在Xamarin中使用C#开发Android应用程序。 In my axml file, I have a LinearLayout with a certain ID that I have assigned a onTouchListener to. 在我的axml文件中,我具有一个LinearLayout,该线性布局具有一个已分配了onTouchListener的特定ID。 The problem is that as long as the buttons I have in my design are on top of the linearLayout, the onTouchListener is never triggered. 问题是,只要我设计中的按钮位于linearLayout的顶部,就不会触发onTouchListener。 If I set one of the buttons to invisible, then it works. 如果我将按钮之一设置为不可见,则它可以工作。

Is it possible to still have the onTouchListener react even though there are Controls in the way? 即使有Control方式,仍可能使onTouchListener做出反应吗?

This is my axml: 这是我的axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_weight="1">
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1"
            android:layout_marginBottom="0.0dp"
            android:background="@android:color/holo_green_light"
            android:soundEffectsEnabled="false" />
        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2"
            android:background="@android:color/holo_blue_light" />
    </LinearLayout>
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1">
        <Button
            android:id="@+id/button3"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="3"
            android:background="@android:color/holo_red_light" />
        <Button
            android:id="@+id/button4"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4"
            android:background="@android:color/holo_orange_light" />
    </LinearLayout>
</LinearLayout>

This is how I set the onTouchlistener for the linearLayout: 这就是我为linearLayout设置onTouchlistener的方式:

LinearLayout linLay = FindViewById<LinearLayout>(Resource.Id.LinearLayout1);
linLay.SetOnTouchListener(new MyTouchListener(this));

This is a custom touch listener class: 这是一个自定义的触摸侦听器类:

    public class MyTouchListener : Java.Lang.Object, View.IOnTouchListener
    {
    Context activityContext;

    public MyTouchListener(Context ac)
    {
        activityContext = ac;
    }

    public bool OnTouch(View v, MotionEvent e)
    {
        float x = 0.0f;
        float y = 0.0f;

        if (e.Action == MotionEventActions.Down)
        {
            // do stuff

            x = e.GetX();
            y = e.GetY();

            Toast.MakeText(activityContext, x.ToString(), ToastLength.Short).Show();


            return true;
        }
        //if (e.Action == MotionEventActions.Up)
        //{
        //    // do other stuff
        //    return true;
        //}
        return true;
    }
}

And finally, this is how I set up the button click listener: 最后,这是我设置按钮点击监听器的方式:

        Button button = FindViewById<Button>(Resource.Id.button1);
        button.Click += (s,e) =>
        {
            //int i = count % s.Count;
            //if(i > 28)
            //    Toast.MakeText(this, button.GetX().ToString(), ToastLength.Short).Show();

            button.Text = string.Format("{0}", count++);

        };

You have to intercept the touch event. 您必须拦截触摸事件。 In order to do that you have to subclass LinearLayout and override onInterceptTouchEvent() : 为此,您必须onInterceptTouchEvent() LinearLayout子类并重写onInterceptTouchEvent()

public class MyLinearLayout extends LinearLayout {
    public MyLinearLayout(Context context) {
        super(context);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MyLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Toast.makeText(getContext(), "onTouch", Toast.LENGTH_SHORT).show();
        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return true;
    }
}

Your xml : 您的xml

<?xml version="1.0" encoding="utf-8"?>
<com.your.package.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/LinearLayout1"
              android:layout_height="fill_parent"
              android:layout_width="fill_parent"
              android:orientation="vertical">
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        android:layout_weight="1">
        <Button
            android:id="@+id/button1"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="1"
            android:layout_marginBottom="0.0dp"
            android:background="@android:color/holo_green_light"
            android:soundEffectsEnabled="false" />
        <Button
            android:id="@+id/button2"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="2"
            android:visibility="invisible"
            android:background="@android:color/holo_blue_light" />
    </LinearLayout>
    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1">
        <Button
            android:id="@+id/button3"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="3"
            android:background="@android:color/holo_red_light" />
        <Button
            android:id="@+id/button4"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="4"
            android:background="@android:color/holo_orange_light" />
    </LinearLayout>
</com.your.package.MyLinearLayout>

Now any touch event on the layout will be handled by your custom MyLinearLayout . 现在,布局上的任何触摸事件都将由您的自定义MyLinearLayout处理。

You need to extend (inherent) the parent linear layout so that you can intercept touch events from the nested children layouts. 您需要扩展(固有)父线性布局,以便可以拦截嵌套子布局中的触摸事件。 Override these two methods to receive touches from the children. 覆盖这两种方法以接收孩子的触摸。 Then in your xml you will add "myCustomLinearLayout" or whatever you name the your custom linear layout class. 然后在您的xml中添加“ myCustomLinearLayout”或您自定义的线性布局类的任何名称。 It should be pretty similar in C# / Xamarin. 在C#/ Xamarin中应该非常相似。

@Override
   public boolean onInterceptTouchEvent(MotionEvent ev) {
     return true; // With this i tell my layout to consume all the touch events from its childs
  }

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    // Log.d(TAG, String.format("ACTION_DOWN | x:%s y:%s", 
        break;
    case MotionEvent.ACTION_MOVE:
    //Log.d(TAG, String.format("ACTION_MOVE | x:%s y:%s", 
        break;
    case MotionEvent.ACTION_UP:
        break;
    }
    return true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM