用两个手指滑动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:应该可以解决您的问题。

文件。

相关内容

最新更新