问题:遍历控件,然后对点击的按钮进行样式的更改,其他组件恢复默认样式,在鸿蒙HarmonyOS中如何实现?请给出具体的代码?
回答: 1、以下代码仅供参考: DependentLayout layout = (DependentLayout)findComponentById(ResourceTable.Id_layout); for (int i = 0; i < layout.getChildCount(); i++) { Component component = layout.getComponentAt(i); if (component instanceof Button) { Button btn = (Button)component; btn.setBackground(new PixelMapElement(getResourceManager().getResource(ResourceTable.Media_icon))); } else if (component instanceof Text) { Text text = (Text)component; text.setBackground(new ShapeElement(this,ResourceTable.Graphic_background_ability_main)); } } 2、以下代码仅供参考: DirectionalLayout layout = (DirectionalLayout)findComponentById(ResourceTable.Id_layout); for (int i = 0; i < layout.getChildCount(); i++) { Component c = layout.getComponentAt(i); if (c instanceof Button) { Button btn = (Button)c; System.out.println(btn); } else if (c instanceof Text) { Text t = (Text)c; System.out.println(t); } } 注:请选择其中一个去测试。 |