一个在之前但不在之后工作的循环

时间:2014-06-23 17:21:30

标签: java android eclipse

我有一个很奇怪的问题。我有三个按钮相同的代码,但它适用于第一个而不是其他按钮。这怎么可能?有人可以向我解释一下吗?非常感谢。 我可以单击按钮(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);    

                }  
                }); 

抱歉缩进

1 个答案:

答案 0 :(得分:0)

您的程序正在存储abc的值,但是对它们进行的唯一更改是让它们递增。点击几下,abc将高于2,永远不会低于2.