【问题标题】:detect a screen touch outside the spinnersearch view在 spinnersearch 视图之外检测屏幕触摸
【发布时间】:2021-06-19 08:15:52
【问题描述】:

我通过 xamarin.android 创建了一个 android 应用程序。我在一个片段中有一个 multispinnersearch,当正常打开时,它里面的所有项目都是预先选择的。但我有一个问题。如果用户触摸微调器外部的屏幕,后者会关闭并且所有项目都会进入我的列表。我不想要那个。除非他在微调器中单击“确定”,否则不应将任何项目带到我的列表中。所以我尝试处理触摸事件以防止选择屏幕触摸上的项目,但它没有用。这是我尝试过的代码:

  public override bool DispatchTouchEvent(MotionEvent ev)
        {
            if (ev.Action == MotionEventActions.Down)
            {
                View v = CurrentFocus;
                if (v is MultiSpinnerSearch)
                {
                    Rect outRect = new Rect();
                    v.GetGlobalVisibleRect(outRect);
                    if (!outRect.Contains((int)ev.RawX, (int) ev.RawY))
                    {
                        Toast.MakeText(this, "shgsg", ToastLength.Long).Show();
                    }
                }
            }
            return base.DispatchTouchEvent(ev);
        }

我在我的主要活动中尝试过这个,但我没有工作。然后我在 ontouch 监听器界面的片段中尝试了这个:

 if (e.Action == MotionEventActions.Down)
            {
                if (labors_dropdown.IsFocused == true)
                {
                    Android.Graphics.Rect rect = new Rect();
                    labors_dropdown.GetGlobalVisibleRect(rect);
                    if (!rect.Contains((int)e.RawX, (int)e.RawY))
                    {
                        Toast.MakeText(this.Context, "gfgf", ToastLength.Short).Show();
                    }
                }
            }

它也不起作用,我该怎么办?提前致谢。

【问题讨论】:

    标签: xamarin.android android-spinner touch-event


    【解决方案1】:

    你可以试试下面的方法:

    public override bool DispatchTouchEvent(MotionEvent ev)
        {
            if (ev.Action == MotionEventActions.Down)
            {
                View v = (MultiSpinnerSearch)FindViewById<MultiSpinnerSearch>(Resource.Id.xxxxx);           
                if (!IsTouchPointInView(v, (int)ev.GetX(), (int)ev.GetY()))
                    {
                        Toast.MakeText(this, "shgsg", ToastLength.Long).Show();
                    }
            }
    
            return base.DispatchTouchEvent(ev);
        }
    
    private bool IsTouchPointInView(View targetView, int currentX, int currentY)
        {
            if (targetView == null)
            {
                return false;
            }
            int[] localtion = new int[2];
            targetView.GetLocationOnScreen(localtion);
            int left = localtion[0];
            int top = localtion[1];
            int right = left + targetView.MeasuredWidth;
            int bottom = top + targetView.MeasuredHeight;
            if (currentY >= top && currentY <= bottom && currentX >= left
              && currentX <= right)
            {
                return true;
            }
            return false;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多