检查Android的所有Invisible按钮行和列

时间:2018-06-18 08:06:08

标签: android button

情况就是这样,我点击我的按钮然后让它隐藏起来Onclick。问题是:如何检查列和行中的所有按钮是否都不可见(没有人留下)。然后执行另一个命令。 以下是我当前可见的一些屏幕截图按钮。

enter image description here

我的代码:

 private void loadCards(){
            try{
                givingcards.start();

                int size = ROW_COUNT*COL_COUNT;

                Log.i("loadCards()","size=" + size);

                ArrayList<Integer> list = new ArrayList<Integer>();

                for(int i=0;i<size;i++){
                    list.add(new Integer(i));
                }


                Random r = new Random();

                for(int i=size-1;i>=0;i--){
                    int t=0;

                    if(i>0){
                        t = r.nextInt(i);
                    }

                    t=list.remove(t).intValue();
                    cards[i%COL_COUNT][i/COL_COUNT]=t%(size/2);

                    Log.i("loadCards()", "card["+(i%COL_COUNT)+
                            "]["+(i/COL_COUNT)+"]=" + cards[i%COL_COUNT][i/COL_COUNT]);
                }
            }
            catch (Exception e) {
                Log.e("loadCards()", e+"");
            }

        }

        private TableRow createRow(int y){
             TableRow row = new TableRow(context);
             row.setHorizontalGravity(Gravity.CENTER);

             for (int x = 0; x < COL_COUNT; x++) {
                     row.addView(createImageButton(x,y));
             }
             return row;
        }

        private View createImageButton(int x, int y){
            Button button = new Button(context);
            button.setBackgroundDrawable(backImage);
            button.setId(100*x+y);
            button.setOnClickListener(buttonListener);
            return button;
        }

2 个答案:

答案 0 :(得分:1)

您可以应用一个简单的解决方案,即使用变量作为计数器,只要调用onClick(),它就会更新

   int counter = 0;

    public void onClick(View view) {
      // visible button ..
      counter++;
      checkButtonsStatus();
    }

    public checkButtonsStatus() {
    if (counter == (4*4)) {
      // all buttons is visible
    }
    }
}

希望这会有所帮助

答案 1 :(得分:0)

试试这段代码:

createImageButton();中添加以下方法:

button.setTag(100*x+y);

buttonListener中添加以下代码:

public void checkCards(){
                if(cards[secondCard.x][secondCard.y] == cards[firstCard.x][firstCard.y]){
                    successpair.start();
                    firstCard.button.setVisibility(View.INVISIBLE);
                    secondCard.button.setVisibility(View.INVISIBLE);
                }
                else {
                    wrongpair.start();
                    secondCard.button.setBackgroundDrawable(backImage);
                    firstCard.button.setBackgroundDrawable(backImage);
                }

                firstCard=null;
                secondCard=null;


                // check here


                boolean isAnyViewVisible = false;

                for (int y = 0; y < ROW_COUNT; y++) {
                    for (int x = 0; x < COL_COUNT; x++) {
                        View view = findViewWithTag(100 * x + y);

                        if (view!=null && view.getVisibility() == VISIBLE) {
                            isAnyViewVisible = true;
                            break;
                        }
                    }
                    if (isAnyViewVisible) {
                        break;
                    }
                }

                if (!isAnyViewVisible) {
                    // write your code here, which you want to execute when all views are invisible

                }


            }

将此方法更改为此方法:

    // check here
    private void turnCard(Button button,int x, int y) {
        button.setBackgroundDrawable(images.get(cards[x][y]));

        if(firstCard==null){
            firstCard = new Card(button,x,y);

// setTag在这里                 firstCard.button.setTag(100 * X + Y);             }             否则{

            if(firstCard.x == x && firstCard.y == y){
                return; //the user pressed the same card
            }

            secondCard = new Card(button,x,y);

// setTag here

            secondCard.button.setTag(100*x+y);

            turns++;
            ((TextView)findViewById(R.id.tv1)).setText("Tries: "+turns);


            TimerTask tt = new TimerTask() {

                @Override
                public void run() {
                    try{
                        synchronized (lock) {
                            handler.sendEmptyMessage(0);
                        }
                    }
                    catch (Exception e) {
                        Log.e("E1", e.getMessage());
                    }
                }
            };

            Timer t = new Timer(false);
            t.schedule(tt, 1300);
        }


    }