0

我有这个代码:

final OnClickListener clickListener = new OnClickListener() {

            private Button buttonClicked;

            public void onClick(View v) {
                Button button = (Button) v;
                button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));

                if (buttonClicked == null) {
                    // first button is clicked
                    buttonClicked = button;
             } else if (buttonClicked.getParent() != button.getParent()) {
                    // second button is clicked
                    if (buttonClicked.getTag().equals(button.getTag())) {
                        Toast.makeText(Spojnice.this, "Correct", Toast.LENGTH_SHORT).show();
                        button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                        buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                        buttonClicked.setEnabled(false);
                        button.setEnabled(false);
                    } else {
                        Toast.makeText(Spojnice.this, "Wrong", Toast.LENGTH_SHORT).show();

                    }
                 buttonClicked = null;
                }       
            }

     };

我按下一个按钮,然后按下另一个按钮,现在它检查两个按钮是否来自同一个父级,如果是,则在 if 语句中继续。但是,如果它们不是来自同一个父级,则代码将 else if 语句退出到 buttonClicked = null;。在这里,我有一个问题。第二次点击后是第三次点击,我需要注销第一次点击,就像它从未发生过一样,因为现在我需要比较第二次和第三次点击。在我的情况下会发生什么,第一次单击一直保持活动状态,并等到 else if 语句的条件检查为正,然后继续执行下一个 if 语句。这对我不利。

那么,如何像从未发生过一样取消注册第一次点击,并只激活最近的两次点击,所以在这种情况下是第 2 次和第 3 次?当我按下第 4 个按钮取消注册第 2 个时。

4

2 回答 2

1

我想这就是你要找的。如果它们没有相同的标签,此代码会将第二个按钮更改为第一个单击的按钮。

//This has to be a global variable so that last clicked button can be retained. 
//Earlier when it was local to the listener so its value was always set to null whenever a button was clicked
private Button buttonClicked=null;

final OnClickListener clickListener = new OnClickListener() {

     public void onClick(View v) {
         Button button = (Button) v;
         button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x003333));

         if (buttonClicked == null) {
                // first button is clicked
                buttonClicked = button;
         } 

//else if (buttonClicked.getParent() != button.getParent()) {//THIS was the culprit
 else if (buttonClicked != button) {//Direct comparison of buttons was the solution                    
// second button is clicked
 if (buttonClicked.getTag().equals(button.getTag())) {
        Toast.makeText(Spojnice.this, "Correct", Toast.LENGTH_SHORT).show();
        button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
        buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
        buttonClicked.setEnabled(false);
        button.setEnabled(false);
        //Moved this line inside if condition so that button click is only set to null when there is a match.
        buttonClicked = null;
        } else {
                            Toast.makeText(Spojnice.this, "Wrong", Toast.LENGTH_SHORT).show();

                        /*
                        * IMPORTANT: change the color here to the original unclicked 
button color. Now in the else condition i.e. there was not a match we need to make the last clicked button to look like an unclicked button so we need to change it to original color and then make the new clicked button to be the current clicked button.
                            */
                            buttonClicked.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x66FF33));
                            //Make the second button the current cliked one
                            buttonClicked = button;

                    }

                }       
            }
     };
于 2013-04-10T03:57:18.767 回答
0

很难理解你到底在做什么,所以我不会用手引导你,而是试着澄清你的算法。我认为伪代码看起来像这样。

When clicking a button with nothing else selected ("clicked button" is null):
    Set the background colour
    Disable the button
    Note clicked button
When clicking a button with its pair "clicked button" selected (different parents, equal tags)
    Set the background colour
    Disable the button
    Note second clicked button
When clicking a button with a non-pair "clicked button" selected (different parents, different tags)
    Reset the "clicked button" background colour
    Enable the "clicked button"
    Set "clicked button" to null
    if "second clicked button" is not null
        Reset the "second clicked button" background colour
        Enable the "second clicked button"
        Set "second clicked button" to null
    Perform processing in "When clicking a button with nothing else selected"

我看到你有很多关于这个问题的问题;我认为,来源是您还没有清楚算法。请注意,此算法意味着您无法从一对按钮中取消选择按钮,因为它们已禁用。想想你将如何允许取消选择 - 如果点击的按钮被启用,那么我们需要处理第二次点击。

于 2013-04-11T03:46:57.830 回答