如何取消注册按钮点击?

时间:2013-04-10 02:19:04

标签: java android

我有这段代码:

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语句。这对我没有好处。

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

2 个答案:

答案 0 :(得分: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;

                    }

                }       
            }
     };

答案 1 :(得分: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"

我见过你在这个问题上有过多个问题;我认为,这个来源是你还没有明确的算法。请注意,此算法意味着您无法取消选择一对按钮,因为它们已禁用。考虑如何允许取消选择 - 如果启用了单击的按钮,那么我们需要处理第二次单击。