Android:批量启用/禁用按钮

发布于 2024-09-08 20:17:51 字数 172 浏览 0 评论 0原文

我有一个活动,其中将一堆按钮放置在 TableLayout 内,就像拨号盘一样。在某些操作过程中,我需要暂时禁用按钮。令我不愉快的是,执行 TableLayout.setEnabled(false) 对嵌套按钮没有任何影响。我是否坚持设置每个单独的按钮,或者是否有一个漂亮(更好)的方法来实现相同的目的?

I have an activity where a bunch of buttons are placed inside TableLayout, not unlike a dial pad. 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. Am I stuck with setting each individual button or is there a nifty (better) way to achieve the same?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

守不住的情 2024-09-15 20:17:51

我会尝试做这样的事情:

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'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);
}
弥枳 2024-09-15 20:17:51

我认为您必须将每个按钮设置为停用。为了使它看起来更好一点,您可以将所有按钮放在一个列表中,并在激活和停用期间迭代它们。但这不会阻止您在代码中一次找到它们。

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.

冧九 2024-09-15 20:17:51

由于按钮嵌套在 TableLayout 下,因此应该很容易迭代子级并设置每个按钮。不知道有没有更简单的方法。

Since the buttons are nested under a TableLayout, it should be easy to iterate over the children and set each one. I don't know if there is an easier way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文