[英]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.