【问题标题】:Why i can't setEnable(false) for created dynamically buttons为什么我不能为创建的动态按钮设置启用(假)
【发布时间】:2014-05-31 23:23:59
【问题描述】:

我正在使用 TableLayout 和 TableRow 创建 6/6 网格按钮。

 private Button myButton;   
    private static final int  a=7;
    private static final int  b=7;
        private void createLayoutDynamically() {
                won = (TableLayout)findViewById(R.id.won);

                for ( int qq = 1; qq < a; qq++) {
                    TableRow tableRow = new TableRow(this);
                    tableRow.setLayoutParams(new TableLayout.LayoutParams(

                            TableLayout.LayoutParams.MATCH_PARENT,
                            TableLayout.LayoutParams.MATCH_PARENT,
                            2
                            ));

                    won.setPadding(20,20,20,20);
                    won.addView(tableRow);

                for ( int q = 1; q < b; q++) {

                    myButton = new Button(this);

        }
        }
        }

我还实现了 CounDownTimme,当时间结束时,我想禁用对网格中所有按钮的 onclick。

public void inittimer(){

         lol =new CountDownTimer(czass, 100) {

             public void onTick(long millisUntilFinished) {

                 czass = millisUntilFinished;
                 czas.setText("" + millisUntilFinished / 1000);


             }

            public void onFinish() {

                layout2.setVisibility(View.VISIBLE);
                for ( int i = 0; i < won.getChildCount();  i++ ){
                    View view = won.getChildAt(i);
                    view.setEnabled(false);
                }

             }
            }.start();

    }

所以我得到了我所有的网格孩子,但是当时间结束时我仍然可以点击这个按钮。为什么他们没有被禁用?你能帮帮我吗?


解决方案:

为了清楚起见,我更正了@matiash 的代码以适合我的代码:

Button buttons [][]= new Button[a][b];
for (int i=1;i<buttons.length;i++) {
                     for (int j=1;j<buttons[i].length;j++) {
                         Button btn = buttons[i][j];
                         btn.setEnabled(false);
                    }
                    }

这样做也一样,但适合我发布的代码。有人能派上用场吗。

【问题讨论】:

    标签: java android android-layout android-button


    【解决方案1】:

    TableLayout 的子视图是您添加的各种 TableRow,而不是按钮本身。

    您可以更改此代码以迭代TableLayout 的所有“孙子”,但我建议使用更简单的替代方法:保留您添加的所有按钮的列表,然后对其进行迭代。例如:

    mAllButtons = new ArrayList<Button>();
    
    for ( int q = 1; q < b; q++) {
        myButton = new Button(this);
        mAllButtons.add(myButton);  
    }
    

    然后,稍后:

    for (Button b : mAllButtons)
        b.setEnabled(false);
    

    【讨论】:

    • 我会测试它,所以当我写 Mybutton.setEnable(false) 时,我禁用了网格的最后一个按钮?
    • 好的,这个想法可行,但我对这段代码做了一些修改。我已经编辑了我的第一篇文章。谢谢你。
    • @JaKoZo 您的代码确实有效,但它似乎比应有的复杂一些。如果您需要遍历 all 按钮,为什么还需要一个矩阵? :)
    • 因为当我点击这个按钮时,它们消失了。当一些指令结束时,我想用例如显示这些按钮。一些价值观。一遍又一遍。
    【解决方案2】:

    发生这种情况是因为getChildAt() 返回View:当调用setEnabled() 时,您正在更改View 的状态,而不是Button

    API reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      • 2010-11-22
      • 1970-01-01
      • 2022-06-18
      • 2019-01-13
      • 1970-01-01
      相关资源
      最近更新 更多