xamarin.识别Xamarin Android和'由于没有窗口焦点而导致的丢弃事件'



我正试图在Xamarin Android智能手表应用程序上处理左滑动、右滑动和双击。

我用这篇文章作为指南,但我似乎不能称之为OnSingleOnLongPressOnSwipeRightOnSwipeLeft。。。

我收到以下输出:

02-17 10:29:36.893 W/ViewRootImpl[MainActivity]( 4450): Dropping event due to no window focus: MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=46.893387, y[0]=209.68695, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=2, eventTime=2834886, downTime=2834684, deviceId=0, source=0x1002 }

我错过了什么或做得不对?

代码:

[Activity(Label = "@string/app_name", MainLauncher = true)]
public class MainActivity : WearableActivity, GestureDetector.IOnGestureListener
{
private GestureDetector gestureDetector;
const int SWIPE_DISTANCE_THRESHOLD = 100;
const int SWIPE_VELOCITY_THRESHOLD = 100;
/// <summary>
/// The on create.
/// </summary>
/// <param name="bundle">
/// The bundle.
/// </param>
protected override async void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
this.SetContentView(Resource.Layout.activity_main);
this.gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener());
TextView text = this.FindViewById<TextView>(Resource.Id.text);
this.SetAmbientEnabled();
while (true)
{
//...code...
await Task.Delay(5000);
//...code...
}
}
public bool OnDown(MotionEvent e)
{
return true;
}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
float distanceX = e2.GetX() - e1.GetX();
float distanceY = e2.GetY() - e1.GetY();
if (Math.Abs(distanceX) > Math.Abs(distanceY) && Math.Abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
{
if (distanceX > 0)
{
this.OnSwipeRight();
}
else
{
this.OnSwipeLeft();
}
return true;
}
return false;
}
void OnSwipeLeft()
{
TextView serviceScoreLabel = this.FindViewById<TextView>(Resource.Id.serviceScore);
serviceScoreLabel.Text = "LLLLL";
}
void OnSwipeRight()
{
TextView serviceScoreLabel = this.FindViewById<TextView>(Resource.Id.serviceScore);
serviceScoreLabel.Text = "RRRRR";
}
public void OnLongPress(MotionEvent e)
{
TextView serviceScoreLabel = this.FindViewById<TextView>(Resource.Id.serviceScore);
serviceScoreLabel.Text = "long press";
}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
}
public void OnShowPress(MotionEvent e)
{
TextView serviceScoreLabel = this.FindViewById<TextView>(Resource.Id.serviceScore);
serviceScoreLabel.Text = "show press";
}
public bool OnSingleTapUp(MotionEvent e)
{
TextView serviceScoreLabel = this.FindViewById<TextView>(Resource.Id.serviceScore);
serviceScoreLabel.Text = "single tap";
return false;
}
public bool OnTouch(View v, MotionEvent e)
{
return gestureDetector.OnTouchEvent(e);
}
}

您应该重写OnTouchEvcent方法,您错过了吗?

[Activity(Label = "SwipActivity",MainLauncher =true)]
public class SwipActivity : Activity, GestureDetector.IOnGestureListener
{
AlertDialog _dialog;
GestureDetector gestureDetector;
const int SWIPE_DISTANCE_THRESHOLD = 100;
const int SWIPE_VELOCITY_THRESHOLD = 100;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.swip);

gestureDetector = new GestureDetector(this);
}
public bool OnDown(MotionEvent e)
{
return true;
}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
float distanceX = e2.GetX() - e1.GetX();
float distanceY = e2.GetY() - e1.GetY();
if (Math.Abs(distanceX) > Math.Abs(distanceY) && Math.Abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
{
if (distanceX > 0)
OnSwipeRight();
else
OnSwipeLeft();
return true;
}
return false;
}
void OnSwipeLeft()
{
Toast.MakeText(this, "Swip left", ToastLength.Short).Show();
}
void OnSwipeRight()
{
Toast.MakeText(this, "Swip right", ToastLength.Short).Show();
}
public void OnLongPress(MotionEvent e)
{
Toast.MakeText(this, "OnLongPress", ToastLength.Short).Show();
}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
}
public void OnShowPress(MotionEvent e)
{
}
public bool OnSingleTapUp(MotionEvent e)
{
return false;
}
public bool OnTouch(View v, MotionEvent e)
{
return gestureDetector.OnTouchEvent(e);
}
public override bool OnTouchEvent(MotionEvent e)
{
gestureDetector.OnTouchEvent(e);
return false;
}
}

最新更新