2016-01-21 29 views
0

我真的有在布局3按钮。我希望当点击一个按钮时,其他2个按钮的样式被设置为“默认”。在我的例子中:背景颜色。 按下按钮1必须改变自己的风格,并复位其他样式的按钮。如何重置其他视图样式(按钮)或一次复位所有按钮的背景布局?

最简单的解决方案是设置各个按钮的样式,每个方法调用,但是这是最简单的解决方案,如果该层只有2-5按钮,但如果将有10-20?

在抽拉/ button_bg.xml我有2状态选择:默认和state_selected。 是否可以立即重置(或设置)图层(布局)中所有按钮的所有样式,而无需重新加载应用程序?

或者,如果没有按下按钮,将默认的风格?或者像layout.AllButtons.setDefaultStyle(true)(对不起) When click on one button, need to change styles for other

正如你现在看到的state_selected没有重新设置后,新的点击其他按钮。

+0

看到这个答案做手工:https://stackoverflow.com/questions/17969925/how-to-highlight-a-button-当被按下时 –

回答

0

您可以通过迭代的布局和设置需要PARAMS像

private void unselectButtons(ViewGroup root, View except) { 
    int childCount = root.getChildCount() 
    for (int i=0; i < childCount; ++i) { 
     View child = root.getChildAt(i); 
     if (child instanceof ViewGroup) { 
     unselectButtons((ViewGroup)child,except); 
     } else if (child instanceof Button && child != except) { 
     child.setSelected(false); 
     } 

    } 


} 
+0

它正在工作。谢谢。但是,只有在方法unselectButtons中单独声明计数'Integer count = root.getChildCount(); for(int i = 0; i ssllav