【问题标题】:Checking all Invisible buttons rows and columns Android [duplicate]检查所有不可见的按钮行和列Android [重复]
【发布时间】:2018-06-20 15:17:58
【问题描述】:

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

我的代码:

 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;
        }

【问题讨论】:

  • 假设你有你传递给适配器的DataModel,你应该在DataModel中有一个字段告诉你它是否是flipped,然后你可以检查是否所有它们是基于此翻转的。
  • 好的,感谢您的回复。但是你能告诉我一个示例代码吗?顺便提一句。我在代码中手动生成了这些按钮。
  • 我可以,如果您发布您传递给适配器的 DataModel 类。也许你的适配器也和当你切换你的视图的visibility
  • 是什么样的布局?是 GridView、RecyclerView 还是简单的布局——RelativeLayout、LinearLayout?
  • 把你生成按钮的代码放上去。

标签: android button


【解决方案1】:

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

   int counter = 0;

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

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

希望对你有帮助

【讨论】:

  • 哇,谢谢哥们。我稍后会试试这个。
  • 不客气,如果您对此解决方案有任何问题,请发表评论
【解决方案2】:

试试这个代码:

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);

//在这里设置标签 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);

//在这里设置标签

            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);
        }


    }

【讨论】:

  • 我尝试了您的代码,但我的应用程序崩溃并停止。
  • 这是什么崩溃?
  • 我改了代码,试试这个
  • 顺便说一句。我已经有了这个执行的 Onclick 监听器,我可以把你的代码放在哪里才能工作?检查一下。
  • 在turnCard()下;在最后一行
猜你喜欢
  • 2011-10-21
  • 2016-10-01
  • 1970-01-01
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-10
  • 2015-08-17
相关资源
最近更新 更多