【问题标题】:Android Disable Grid of ButtonsAndroid禁用按钮网格
【发布时间】:2016-04-03 09:50:50
【问题描述】:

我有一个网格,里面有很多按钮,是否可以禁用网格,这会导致所有按钮都被禁用?

原因是在用户单击按钮回答问题后,检查答案并计算分数等,但如果我在发生这种情况时继续按下按钮,它会继续回答下一个问题,依此类推,即使我使用处理程序等待。禁用只是暂时的,它们会在计算完所有内容后再次启用。

我该如何阻止呢?无需手动禁用每个按钮。

提前致谢。

编辑:

                GridLayout numgrid = (GridLayout) findViewById(R.id.numbersGrid);
            GridLayout lettergrid = (GridLayout) findViewById(R.id.lettersGrid);
            numgrid.setEnabled(false);
            lettergrid.setEnabled(false);

我试过上面的代码不起作用,因为我也需要它。

【问题讨论】:

    标签: java android button


    【解决方案1】:

    显然似乎没有一种“简单”的方法来解决这个问题。您可以做的是将布局中的所有按钮作为一个数组抓取,然后循环遍历它们。类似于以下方法,它会禁用 GridLayout 中的所有按钮:

    private void disableButtons(GridLayout layout) {
    
        // Get all touchable views
        ArrayList<View> layoutButtons = layout.getTouchables();
    
        // loop through them, if they are an instance of Button, disable it.
        for(View v : layoutButtons){
            if( v instanceof Button ) {
                ((Button)v).setEnabled(false);
            }
        }
    }
    

    然后只需传入您的网格布局:

    GridLayout numGrid = (GridLayout) findViewById(R.id.numbersGrid);
    GridLayout letterGrid = (GridLayout) findViewById(R.id.lettersGrid);
    
    disableButtons(numGrid);
    disableButtons(letterGrid);
    

    【讨论】:

      猜你喜欢
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 1970-01-01
      • 2011-05-22
      • 2011-02-16
      相关资源
      最近更新 更多