简体   繁体   English

当所有其他按钮被禁用时,如何启用某些按钮?

[英]How to enable some buttons, when all others are disabled?

I have 16 buttons in my game activity. 我的游戏活动中有16个按钮。 Plus 4 (I will talk about them later), all 4 disabled at this point. 加4(我将在稍后讨论),此时所有4个禁用。 Now, whenever a user click on one of those 16 buttons, I set some text to it and then disable it, so it cannot be used again. 现在,每当用户点击这16个按钮中的一个时,我会为其设置一些文本然后将其禁用,因此无法再次使用。 Is there a way, when all 16 buttons are clicked and disabled, to enable all of those 4 buttons? 有没有办法,当点击并禁用所有16​​个按钮时,启用所有这4个按钮?

Here is a quick way to do it: 这是一个快速的方法:

public class example{
    int activeButtons = 16;

    public void onCreate(Bundle savedInstanceState){
        //initialize
    }

    /**
    * This is your onClick method
    */
    public void click(View v){
        v.setEnabled(false);
        activeButtons--;
        if(activeButtons == 0){
            enable();
        }
    }

    /**
    * This will enable your four buttons
    */
    public void enable(){
        //Get references to your buttons here
        Button b1 = ...;
        Button b2 = ...;
        Button b3 = ...;
        Button b4 = ...;

        b1.setEnabled(true);
        b2.setEnabled(true);
        b3.setEnabled(true);
        b4.setEnabled(true);
   }
}

An example of one way to accomplish what you are asking is simply to set aside a case such as this in your code; 完成所要求的一种方法的一个例子就是在代码中留出这样的案例; if I understand what your question is in regards to what has been posted above: 如果我理解你的问题是关于上面发布的内容:

Button buttonOne = null;
//...fourteen buttons
Button otherButtonOne = null;
//...other four buttons

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupButtons();
}

private void setupButtons()
{
    buttonOne.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //..Whatever else you do with button.
            if(allButtonsAreDisabled())
            {
                enableOtherFourButtons();
            }
        }
    });
    //...
}

private boolean allButtonsAreDisabled()
{
    if(!buttonOne.isEnabled()) // && !buttonTwo.isEnabled() && ...
    {
        return true;
    }
    else
        return false;
}

private void enableOtherFourButtons()
{
    otherButtonOne.setEnabled(true);
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较所有按钮? 或者检查是否全部被禁用? - How to compare all the buttons? Or check if all are disabled? 在布局中禁用按钮后,如何以编程方式启用按钮 - How to enable a button programmatically when it is disabled in the layout 如何在更改某些JButton的背景颜色而不是其他颜色时使JButton看起来一样? - How do I make the JButtons all look the same when changing background color on some of them and not on others? 禁用ScrolledComposite内部的CheckboxTableViewer时如何启用滚动? - How to enable scroll when CheckboxTableViewer inside a ScrolledComposite is disabled? 如何只保留一些消息头并删除所有其他消息头? - How to keep only some message headers and remove all others? 询问位置时禁用按钮 - Buttons disabled when asking for location 如何启用命令按钮 - How to enable a command buttons 如何通过鼠标单击启用一个JButton,然后通过迭代禁用所有其他JButton? - How to enable one JButton by mouse click and then disable all others through iteration? 如何保留一些图纸而不保留其他图纸? - How to keep some drawings and not others? 当值不为null时如何启用javax注释,而当值为null时如何禁用javax注释? - How to enable javax annotations when value not null and disabled when value is null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM