基于最后点击的按钮创建IF语句的最佳方法

时间:2014-08-02 17:38:42

标签: java android if-statement android-button buttonclick

我有一个Button,它有一些IF语句,根据选中的Toggle按钮改变它的功能。这一切都很好。 我现在想要创建一个声明,根据是否在它之前单击某个按钮来改变功能。任何想法都会很棒。

这是我到目前为止按钮

的代码

// YellowButton

    player1Counter= 0;
    player2Counter= 0;
    breakCounter= 0;
    yellowAdd2 = (Button) findViewById(R.id.yellowButton);
    player1 = (ToggleButton) findViewById(R.id.player1Button);
    player2 = (ToggleButton) findViewById(R.id.player2Button);
    player1ScoreDisplay= (TextView) findViewById(R.id.player1Score);
    player2ScoreDisplay= (TextView) findViewById(R.id.player2Score);
    breakDisplay= (TextView) findViewById(R.id.breakCounterNum);

    yellowAdd2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
    if (toggle1.isChecked()) {
        if (player1.isChecked()) {
            player1Counter += 2;
            breakCounter += 2;
            player1ScoreDisplay.setText("" + player1Counter);
            breakDisplay.setText("" +breakCounter);
        }
        if (player2.isChecked()) {
            player2Counter += 2;
            breakCounter += 2;
            player2ScoreDisplay.setText("" + player2Counter);
            breakDisplay.setText("" +breakCounter);
        }
    }
    if (toggle2.isChecked()) {
        if (player1.isChecked()) {
            breakCounter = 0;
            breakDisplay.setText("" +breakCounter);
            player1.setChecked(false);
            player2.setChecked(true);
            toggle2.setChecked(false);
            toggle1.setChecked(true);
        }
        else if (player2.isChecked()) {
            breakCounter = 0;
            breakDisplay.setText("" +breakCounter);
            player2.setChecked(false);
            player1.setChecked(true);
            toggle2.setChecked(false);
            toggle1.setChecked(true);
        }
    }
    if (toggle3.isChecked()) {
        if (player1.isChecked()) {
            breakCounter = 0;
            breakDisplay.setText("" +breakCounter);
            player1.setChecked(false);
            player2.setChecked(true);
        }
        else if (player2.isChecked()) {
            breakCounter = 0;
            breakDisplay.setText("" +breakCounter);
            player2.setChecked(false);
            player1.setChecked(true);
        }
    }
    if (toggle4.isChecked()) {
        if (player1.isChecked()) {
            player2Counter += 4;
            player2ScoreDisplay.setText("" + player2Counter);
            breakCounter = 0;
            breakDisplay.setText("" +breakCounter);
            player2.setChecked(true);
            player1.setChecked(false);
            toggle4.setChecked(false);
            toggle1.setChecked(true);
        }
        else if (player2.isChecked()) {
                 player1Counter += 4;
                 player1ScoreDisplay.setText("" + player1Counter);
                 breakCounter = 0;
                 breakDisplay.setText("" +breakCounter);
                 player1.setChecked(true);
                 player2.setChecked(false);
                 toggle4.setChecked(false);
                 toggle1.setChecked(true);
        }
    }
}
}); 

1 个答案:

答案 0 :(得分:0)

按下按钮时可以设置标志:

boolean preButtonWasPressed = false;
// snip

// snap
preButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        preButtonWasPressed = true;
    }
});

稍后检查此标志:

if ( preButtonWasPressed ) {
    // do some stuff
    // clear the flag
    preButtonWasPressed = false;
}

或者,如果按下按钮,您可以更改onClickListener

changeBehaviorButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        yellowAdd2.setOnClickListener(aDifferentOnClickListener);
    }
});