[英]Launch application on 2 finger swipe ANDROID
我有此代码,不会返回任何错误。 但是,它并不像预期的那样工作。 当我用两根手指向上滑动但没有任何反应时,我正在尝试启动一个应用程序。 谁能看到我的代码有什么问题?
public class HomeActivity extends Activity {
ImageButton phoneButton;
ImageButton contactsButton;
ImageButton messagesButton;
ImageButton cameraButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
setTitle("Apps");
loadApps();
loadListView();
addClickListener();
addphoneButtonListener();
addmessagesButtonListener();
addcontactsButtonListener();
addcameraButtonListener();
//Two-Finger Drag Gesture detection
RelativeLayout TextLoggerLayout = (RelativeLayout)findViewById(R.id.mainLayout);
TextLoggerLayout.setOnTouchListener(
new RelativeLayout.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent m) {
handleTouch(m);
return true;
}
});
}
void handleTouch(MotionEvent m){
//Number of touches
int pointerCount = m.getPointerCount();
if(pointerCount == 2){
int action = m.getActionMasked();
switch (action)
{
case MotionEvent.ACTION_DOWN:
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.google.android.googlequicksearchbox");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
}
}
else {
//do something
}
}
尝试调试handleTouch()
来查看其未触发的确切原因。 我的猜测是因为您有对MotionEvent.ACTION_DOWN的检查,在现实世界中,用两根手指同时精确地进行操作有点困难。
将支票移至MotionEvent.ACTION_MOVE(无论如何,这都是您想要的,因为您要滑动而不是点击),然后重试。
MotionEvent.ACTION_DOWN
表示一根手指。 MotionEvent.ACTION_POINTER_DOWN
表示辅助触摸(例如,两个手指)。
因此,将代码移到case MotionEvent.ACTION_POINTER_DOWN:
应该可以解决您的问题。
文件资料 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.