检查是否释放了多个按钮 android studio/java

Check if multiple buttons are released android studio/java

我希望我的程序仅在释放所有按钮时发送 0。 当我释放按钮时,代码现在发送 0,但是如果我按住按钮并释放其中一个,它将发送 0,但我不希望它这样做,我只希望在所有按钮都被释放时发送。 谢谢

@Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (view.getId()){
            case R.id.commend1B:
                if (motionEvent.getAction() == ACTION_DOWN) {
                    commend1BHold = true;
                    Log.i("sending", "sending 1");
                    bluetooth.write("1#");
                } else if (motionEvent.getAction() == ACTION_UP){
                    Log.i("sending", "sending 0");
                    bluetooth.write("0#");
                }
                break;
            case R.id.commend2B:
                if (motionEvent.getAction() == ACTION_DOWN) {
                    Log.i("sending", "sending 2");
                    bluetooth.write("2#");

                } else if (motionEvent.getAction() == ACTION_UP){
                    Log.i("sending", "sending 0");
                    bluetooth.write("0#");
                }
                break;
            case R.id.commend3B:
                if (motionEvent.getAction() == ACTION_DOWN) {
                    Log.i("sending", "sending 3");
                    bluetooth.write("3#");
                } else if (motionEvent.getAction() == ACTION_UP){
                    Log.i("sending", "sending 0");
                    bluetooth.write("0#");
                }
                break;
            case R.id.commend4B:
                if (motionEvent.getAction() == ACTION_DOWN) {
                    Log.i("sending", "sending 4");
                    bluetooth.write("4#");
                } else if (motionEvent.getAction() == ACTION_UP){
                    Log.i("sending", "sending 0");
                    bluetooth.write("0#");
                }
                break;


            default:
                throw new IllegalStateException("Unexpected value: " + view.getId());
        }

        return false;
    }

每次释放按钮(任何按钮)时,调用一个方法来检查是否释放了所有按钮。如果是,请发送 0.

还可以对按下的按钮进行计数:每按下一个按钮,计数器就会增加,反之亦然。如果计数器为零,则发送 0.

我为每个按钮创建了布尔值来检查它是否被按下,当我按下它时它会是真的,当我释放它时它会变成假。当我释放它时,它不会发送 0,而是检查是否每个按钮都没有被按下(检查所有布尔值是否为假),然后才发送 0。

@Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (view.getId()){
            case R.id.commend1B:
                if (motionEvent.getAction() == ACTION_DOWN) {
                    commend1BHold = true;
                    Log.i("sending", "sending 1");
                    bluetooth.write("1#");
                } else if (motionEvent.getAction() == ACTION_UP){
                    commend1BHold = false;
                    send0();

                }
                break;
            case R.id.commend2B:
                if (motionEvent.getAction() == ACTION_DOWN) {
                    commend2BHold = true;
                    Log.i("sending", "sending 2");
                    bluetooth.write("2#");

                } else if (motionEvent.getAction() == ACTION_UP){
                    commend2BHold = false;
                    send0();
                }
                break;
            case R.id.commend3B:
                if (motionEvent.getAction() == ACTION_DOWN) {
                    commend3BHold = true;
                    Log.i("sending", "sending 3");
                    bluetooth.write("3#");
                } else if (motionEvent.getAction() == ACTION_UP){
                    commend3BHold = false;
                    send0();
                }
                break;
            case R.id.commend4B:
                if (motionEvent.getAction() == ACTION_DOWN) {
                    commend4BHold = true;
                    Log.i("sending", "sending 4");
                    bluetooth.write("4#");
                } else if (motionEvent.getAction() == ACTION_UP){
                    commend4BHold = false;
                    send0();
                }
                break;


            default:
                throw new IllegalStateException("Unexpected value: " + view.getId());
        }

        return false;
    }
    public void send0(){
        if(!commend1BHold &&!commend2BHold && !commend3BHold && !commend4BHold){
            Log.i("sending", "sending 0");
            bluetooth.write("0#");
        }
    }