识别Xamarin Android和';由于没有窗口焦点而丢弃事件';
我正在尝试在Xamarin Android smartwatch应用程序上处理左扫、右扫和双击 我已经用过作为指导,但是我似乎不能调用识别Xamarin Android和';由于没有窗口焦点而丢弃事件';,android,xamarin.android,swipe,gesture,touch-event,Android,Xamarin.android,Swipe,Gesture,Touch Event,我正在尝试在Xamarin Android smartwatch应用程序上处理左扫、右扫和双击 我已经用过作为指导,但是我似乎不能调用OnSingle,OnLongPress,onsweepright,onsweeleft 我收到以下输出: 02-17 10:29:36.893 W/ViewRootImpl[MainActivity]( 4450): Dropping event due to no window focus: MotionEvent { action=ACTION_MOVE,
OnSingle
,OnLongPress
,onsweepright
,onsweeleft
我收到以下输出:
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 }
我遗漏了什么或做得不对
代码:
[活动(标签=“@string/app\u name”,MainLauncher=true)]
公共类主活动:WearableActivity、GestureDetector.IOnGestureListener
{
私人手势检测器;
常量整数滑动距离阈值=100;
常量整数滑动速度阈值=100;
///
///正在创建。
///
///
///包裹。
///
受保护的重写异步void OnCreate(捆绑包)
{
base.OnCreate(bundle);
this.SetContentView(Resource.Layout.activity_main);
this.gestureDetector=新的gestureDetector(this,new gestureDetector.SimpleOnGestureListener());
TextView text=this.findviewbyd(Resource.Id.text);
this.SetAmbientEnabled();
while(true)
{
//…代码。。。
等待任务。延迟(5000);
//…代码。。。
}
}
公共厕所(运动事件e)
{
返回true;
}
公共bool OnFling(MotionEvent e1、MotionEvent e2、float velocityX、float velocityY)
{
浮点距离x=e2.GetX()-e1.GetX();
浮点距离y=e2.GetY()-e1.GetY();
if(Math.Abs(distanceX)>Math.Abs(distanceY)&&Math.Abs(distanceX)>滑动距离(SWIPE\u DISTANCE\u THRESHOLD)&&Math.Abs(velocityX)>滑动速度(SWIPE\u THRESHOLD)
{
如果(距离X>0)
{
这是正确的();
}
其他的
{
这个.onswipleft();
}
返回true;
}
返回false;
}
void onsweep left()
{
TextView-serviceScoreLabel=this.FindViewById(Resource.Id.serviceScore);
serviceScoreLabel.Text=“LLLLL”;
}
void onswipright()
{
TextView-serviceScoreLabel=this.FindViewById(Resource.Id.serviceScore);
serviceScoreLabel.Text=“RRRRR”;
}
公开无效在线新闻(运动事件e)
{
TextView-serviceScoreLabel=this.FindViewById(Resource.Id.serviceScore);
serviceScoreLabel.Text=“长按”;
}
公共bool OnScroll(运动事件e1、运动事件e2、浮动距离X、浮动距离Y)
{
返回false;
}
在ShowPress上公开作废(运动事件e)
{
TextView-serviceScoreLabel=this.FindViewById(Resource.Id.serviceScore);
serviceScoreLabel.Text=“显示按”;
}
公共bool onsingletapp(运动事件e)
{
TextView-serviceScoreLabel=this.FindViewById(Resource.Id.serviceScore);
serviceScoreLabel.Text=“单点击”;
返回false;
}
公共bool OnTouch(视图v,运动事件e)
{
返回手势检测器。开触点(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;
}
}
我添加了这个,我可以进入那个块,它只返回false。如何查找是否存在向左滑动、向右滑动、双击等?@Bhav如果向左滑动,它将处理OnSwipeLeft方法,如果向右滑动,它将处理OnSwipeRight方法等等,您可以使用Toast来显示消息,如上所述,我更新了答案。我刚刚复制了您的代码,看起来它只是在执行OnTouchEvent。我没有看到任何消息。如果我在OnTouchEvent中添加一条消息,我可以看到。@Bhav我更新了完整的示例代码,您可以检查,它对我有效,因为我已经比较了代码。在我创建smartwatch应用程序时,我的类实现了
WearableActivity
,而不是Activity
。使用您的代码,与之前发生的情况相同,我输入了OnTouchEvent
,但没有其他方法。我想知道我的代码中的任务.Delay(5000)
是否会阻止任何东西?
[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;
}
}