简体   繁体   English

Android:批量启用/禁用按钮

[英]Android: mass enable/disable buttons

I have an activity where a bunch of buttons are placed inside TableLayout, not unlike a dial pad. 我有一个活动,其中一堆按钮放在TableLayout内,与拨号盘不同。 During some operations I need to temporarily disable the buttons. 在某些操作中,我需要暂时禁用按钮。 To my unpleasant surprise doing TableLayout.setEnabled(false) has no effect on the nested buttons. 令我不愉快的是,做TableLayout.setEnabled(false)对嵌套按钮没有影响。 Am I stuck with setting each individual button or is there a nifty (better) way to achieve the same? 我是坚持设置每个单独的按钮还是有一个漂亮(更好)的方式来实现相同的?

I'd try to do something like this: 我会尝试这样做:

TableLayout tableLayoutInstance; // let's suppouse you have already initialized it
// blablabla
// example to deactivate all buttons
ArrayList<View> touchables = tableLayoutInstance.getTouchables();
for(View touchable : touchables){
    if( touchable instanceof Button )
        ((Button)touchable).setEnabled(false);
}

I think you have to set each individual of this Buttons to deactivated. 我认为你必须将这个按钮的每个人都设置为停用。 To make it look a little bit nicer you could put all of the buttons in a list and iterate over them during activating and deactivating. 为了使它看起来更好一些,您可以将所有按钮放在列表中,并在激活和停用期间迭代它们。 But this will not prevent you from finding them all once in you code. 但这不会阻止您在代码中找到它们。

Since the buttons are nested under a TableLayout, it should be easy to iterate over the children and set each one. 由于按钮嵌套在TableLayout下,因此应该很容易迭代子项并设置每个子项。 I don't know if there is an easier way. 我不知道是否有更简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM