【问题标题】:Android: mass enable buttons which are already disabledAndroid:已禁用的批量启用按钮
【发布时间】:2012-05-03 17:05:02
【问题描述】:

我已经尝试使用下面链接中提到的代码来“批量禁用”按钮,它工作得很好。但是,相同的代码不适用于批量启用。

Android: mass enable/disable buttons

禁用代码(工作)

TableLayout tl = (TableLayout)findViewById(R.id.table1); // 
ArrayList<View> touchables = tl.getTouchables();
for(View touchable : touchables){
if( touchable instanceof Button && touchable != btnNewWord )
((Button)touchable).setEnabled(false);
}

启用代码(不工作)

btnNewWord.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

TableLayout tl = (TableLayout)findViewById(R.id.table1);  
ArrayList<View> touchables = tl.getTouchables();
for(View touchable : touchables){
if( touchable != btnNewWord )
((Button)touchable).setEnabled(true);
}                       

【问题讨论】:

  • 你有什么错误吗?一旦禁用按钮并尝试再次获取 ArrayList,请尝试记录您的 ArrayList 以检查它是否包含值??

标签: android button android-tablelayout


【解决方案1】:

一旦您将按钮设置为禁用,我想,它们将不再是可触摸的。因此,您需要在代码中修改该点并使用其他内容来获取所有视图。您可以保留用于禁用按钮的ArrayList,然后使用它重新启用它们。

编辑:

试试这个:

ArrayList<View> touchables //declare globaly

然后

TableLayout tl = (TableLayout)findViewById(R.id.table1); // 
touchables = tl.getTouchables();
for(View touchable : touchables)
{
    if( touchable instanceof Button && touchable != btnNewWord )
      ((Button)touchable).setEnabled(false);
}

现在,

btnNewWord.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

       for(View touchable : touchables)
       {
          if( touchable != btnNewWord )
            ((Button)touchable).setEnabled(true);
       }  
   }
}                     

【讨论】:

  • 实际上我禁用了所有按钮,除了一个仍然启用的按钮,即 btnNewWord: if( touchable instanceof Button && touchable != btnNewWord ) 我希望用户单击该特定按钮以重新启用所有按钮。
  • 这意味着,你有 btnNewWord 是控制所有其他按钮的残疾的按钮。对吗?那么你需要像你一样在 ArrayList 中保存所有按钮(当它们被启用时),然后你必须使用相同的 ArrayList 来重新启用 btnNewWord 的 onClick() 中的按钮。
  • 非常感谢您的快速响应和努力将解释传达给我。虽然代码仍然无法正常工作,但我现在可以理解问题并正在尝试解决方法。
  • 感谢所有...在我遵循 Hiral 的输入后它工作了
猜你喜欢
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多