一个之前有效但之后无效的循环



我有一个奇怪的问题。我有三个按钮相同的代码,但它适用于第一个,而不是其他的。这怎么可能?有人能给我解释一下吗?非常感谢。我可以点击按钮(B和C)一个,而不是两次。这是怎么呢在第一种情况下(按钮A),它工作,但它只显示一张牌,而不是两张。

@SuppressLint("Recycle")  
public class GameActivity extends Activity {  
protected View buttonA;  
protected View buttonB;  
protected View buttonC;  
protected View buttonD;  
final int randomInt1 = 0;  
final int randomInt2 = 0;  
int a = 0;  
int b = 0;  
int c = 0;  
int d = 0;  
int e = 0;  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_game);  
    findViewById(R.id.A);  
    Resources res = getResources();  
    String[] StringDeck = getResources().getStringArray(R.array.StringDeck);  
    int[] DeckDraw = getResources().getIntArray(R.array.DeckDraw);  
    final HashMap<String, Integer> DECK = new HashMap<String, Integer>();  
    for(int i = 0; i < DeckDraw.length; i++){  
        DECK.put(StringDeck[i], DeckDraw[i]);  
    }  
    final TypedArray Deck = res.obtainTypedArray(R.array.DeckDraw);  
    final ViewGroup groupButtons1 = (ViewGroup)findViewById(R.id.linearLayout1);  
View v1;  
final Button backgroundA = (Button) findViewById(R.id.A);  
final Button backgroundB = (Button) findViewById(R.id.B);  
final Button backgroundC = (Button) findViewById(R.id.C);  
final Button backgroundD = (Button) findViewById(R.id.D);  
final Button backgroundE = (Button) findViewById(R.id.E);  
final Random random1 = new Random();  
final Random random2 = new Random();  
for(int i = 0; i < groupButtons1.getChildCount(); i++) {  
    v1 = groupButtons1.getChildAt(i);  
    if(v1 instanceof Button) v1.setOnClickListener(new Button.OnClickListener() {  

        @SuppressLint("CutPasteId") //Legato ai button inferiori  
        public void onClick(View view){  
        backgroundA.setOnClickListener(new View.OnClickListener(){  
            public void onClick(View view){  
                if(a < 2){  
                int randomInt1 = random1.nextInt(Deck.length());  
                int drawableIDA = Deck.getResourceId(randomInt1, -1);  
                backgroundA.setBackgroundResource(drawableIDA);  
                a ++;  
            }  
            }  
        });  

    backgroundB.setOnClickListener(new View.OnClickListener(){  
        public void onClick(View view){  
            if (b < 2){    
                int randomInt2 = random1.nextInt(Deck.length());  
                int drawableIDB = Deck.getResourceId(randomInt2, -1);  
                backgroundB.setBackgroundResource(drawableIDB);  
                b ++;  
            }  
            backgroundB.setEnabled(false);  
        }             
        });  
            backgroundC.setOnClickListener(new View.OnClickListener(){  
                public void onClick(View view){  
                    if (c < 2){    
                        int randomInt3 = random1.nextInt(Deck.length());  
                        int drawableIDC = Deck.getResourceId(randomInt3, -1);  
                        backgroundC.setBackgroundResource(drawableIDC);  
                        c ++;  
                    }  
                    backgroundC.setEnabled(false);    
                }  
                }); 

抱歉缩进

您的程序正在存储a, b, c的值,但唯一要对它们进行的更改是使它们增加。点击几下后,abc会在2以上,并且永远不能降到2以下。

相关内容

最新更新