更多内容请访问:Harmonyos技术社区https://harmonyos.51cto.com,与华为官方共同建立不同的效果与状态相关联,主要用于设置控件背景以及字体颜色等。控件一共有7种状态,常用的有:空状态、按下状态pressed、focusedfocus、checked状态checked、unavailable状态disabled。鸿蒙中的选择器效果可以通过xml的state-container标签实现,也可以在代码中使用StateElement实现。以按钮的背景选择器为例进行说明://表示当前控件处于选中状态(checkstate),如控件CheckboxpublicstaticfinalintCOMPONENT_STATE_CHECKED=64;//表示当前控件处于不可用状态,如Button的setEnabled为falsepublicstaticfinalintCOMPONENT_STATE_DISABLED=32;//初始状态publicstaticfinalintCOMPONENT_STATE_EMPTY=0;//表示当前控件处于获得焦点状态,如点击TextField输入文字时publicstaticfinalintCOMPONENT_STATE_FOCUSED=2;//表示光标移动到当前控件的状态publicstaticfinalintCOMPONENT_STATE_HOVERED=268435456;//当用户点击或触摸控件的状态,比如Button点击publicstaticfinalintCOMPONENT_STATE_PRESSED=16384;//表示控件被选中的状态,比如作为ListContainer获取焦点(focus),并使用方向键选择其中一个项目(选择器)publicstaticfinalintCOMPONENT_STATE_SELECTED=4;实现与使用:1在resources/base/graphic中创建selector.xml自定义形状,定义不同状态的背景,然后组合多个形状在state-container中配置,为控件生成新的selector_button.xml文件使用。在空状态定义shape背景生成background_btn_empty.xml,其他状态类似:定义按下状态的背景生成background_btn_pressed.xml:创建selector_button.xml,在ohos:element字段配置shape背景,ohos:state配置控件状态:也可以直接使用颜色:2控件使用控件的设置background_element作为状态选择器,选择在状态中根据需要添加不同的状态和不同状态的背景:"5vp"ohos:text="Buttonxml"ohos:text_size="50"/>3代码中,在不同的状态下使用新的ShapeElement,并添加到StateElement中,将需要设置状态选择器的部分设置为添加状态的StateElement,如Button的setBackground,Button的setButtonElementCheckbox,设置后相关控件会有状态选择器的效果3.1Button/***添加一个Button*/privatevoidinitButton(){//Button处于按下的状态);element10);//Disabled状态下Button的elementShapestateElementButtonDisable=newShapeElement();elementButtonDisable.setRgbColor(RgbPalette.GREEN);elementButtonDisable.setShape(ShapeElement.RECTANGLE);elementButtonDisable.setCornerRadius(10)inEmpty状态elementShapeElementelementButtonEmpty=newShapeElement();elementButtonEmpty.setRgbColor(RgbPalette.BLUE);elementButtonEmpty.setShape(ShapeElement.RECTANGLE);elementButtonEmpty.setCornerRadius(10);//Button在Hoveered状态下的elementShaoverElemented=的elementShaoverElementnewShapeElement();elementButtonHovered.setRgbColor(RgbPalette.GRAY);elementButtonHovered.setShape(ShapeElement.RECTANGLE);elementButtonHovered.setCornerRadius(10);//给StateElemen在各个状态添加不同的背景t中StateElementstateElement=newStateElement();stateElement.addState(newint[]{ComponentState.COMPONENT_STATE_PRESSED},elementButtonPressed);stateElement.addState(newint[]{ComponentState.COMPONENT_STATE_DISABLED},elementButtonDisable);stateElement.addState(newint[]{ComponentState.COMPONENT_STATE_HOVERED},elementButtonHovered);stateElement.addState(newint[]{ComponentState.COMPONENT_STATE_EMPTY},elementButtonEmpty);//新建一个button并将其添加到布局中Buttonbutton=newButton(this);button.setMarginTop(20);button.setText("Button");button.setTextSize(50);//设置按键的状态,若为false,则显示状态为COMPONENT_STATE_DISABLED//button.setEnabled(false);button.setBackground(stateElement);DirectionalLayout.LayoutConfiglayoutConfig=newDirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,ComponentContainer.LayoutConfig.MATCH_CONTENT);layoutConfig.alignment=LayoutAlignment.CENTER;layoutConfig.setMargins(20,20,20,20);button.setLayoutConfig(layoutConfig);dlSelector.addComponent(button);}3.2TextField/***添加一个TextField*/privatevoidinitTextField(){//TextField处于按下状态的elementShapeElementelementTextFieldPressed=newShapeElement();elementTextFieldPressed.setRgbColor(RgbPalette.RED);elementTextFieldPressed.setShape(ShapeElement.RECTANGLE);elementTextFieldPressed.setCornerRadius(10);//TextField处于Disabled状态下的elementShapeElementelementTextFieldDisable=newShapeElement();elementTextFieldDisable.setRgbColor(RgbPalette.GRAY);elementTextFieldDisable.setShape(ShapeElement.RECTANGLE);elementTextFieldDisable.set1CornerRadius();/TextField在Empty状态下的elementShapeElementelementTextFieldEmpty=newShapeElement();elementTextFieldEmpty.setRgbColor(RgbPalette.BLUE);elementTextFieldEmpty.setShape(ShapeElement.RECTANGLE);elementTextFieldEmpty.setCornerRadius(10);//TextField在FocusedFieldElement下ShapedShapedShapedShapementShape();elementTextFieldFocused.setRgbColor(RgbPalette.GREEN);elementTextFieldFocused.setShape(形状Element.RECTANGLE);elementTextFieldFocused.setCornerRadius(10);//给StateElement添加不同状态下的不同背景StateElementstateElement=newStateElement();stateElement.addState(newint[]{ComponentState.COMPONENT_STATE_PRESSED},elementTextFieldPressed);stateElement.addState(newint[]{ComponentState.COMPONENT_STATE_DISABLED},elementTextFieldDisable);stateElement.addState(newint[]{ComponentState.COMPONENT_STATE_FOCUSED},elementTextFieldFocused);stateElement.addState(newint[]{ComponentState.COMPONENT_STATE_EMPTY},elementText)/新建一个TextField和添加到布局中TextFieldtextField=newTextField(this);textField.setText("TextField");textField.setTextSize(50);//设置textfield的状态,如果为false,表示状态为COMPONENT_STATE_DISABLED//textField.setEnabled(false);textField.setBackground(stateElement);DirectionalLayout.LayoutConfiglayoutConfig=newDirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,ComponentContainer.LayoutConfig.MATCH_CONTENT);layoutConfig.alignment=LayoutAlignment.CENTER;layoutConfig.setMargins(20,20,20,20);textField.setLayoutConfig(layoutConfig);dlSelector.addComponent(textField);}3.3Checkbox/***添加一个Checkbox*/privatevoidinitCheckbox(){//Checkbox在按下状态的elementShapeElementelementCheckboxPressed=newShapeElement();elementCheckboxPressed.setRgbColor(RgbPalette.RED);elementCheckboxPressed.setShape(ShapeElement.RECTANGLE);elementCheckboxPressed.setCornerRadius(10);//Checkbox在Disabled状态下的elementShapeElementelementCheckboxDisable=newShapeElement();elementCheckboxDisable.setRgbColor(RgbPalette.GREEN);elementCheckboxDisable.setShape(ShapeElement.RECTANGLE);elementCheckboxDisable.setCornerRadius(10);//Checkbox处于空状态下的elementShapeElementelementCheckboxEmpty=newShapeElement();elementCheckboxEmpty.setRgbColor(RgbPalette.BLUE);elementCheckboxEmptysetShape(ShapeElement.RECTANGLE);elementCheckboxEmpty.setCornerRadius(10);//Checkbox处于Checked状态下的elementShapeElementelementCheckboxChecked=newShapeElement();elementCheckboxChecked.setRgbColor(RgbPalette.GRAY);elementCheckboxChecked.setShape(ShapeElement.RECTANGLE);elementCheckboxChecked.setCornerRadius(10);//给StateElement添加不同状态的背景StateElementstateElement=newStateElement();stateElement.addState(newint)[]{ComponentState.COMPONENT_STATE_PRESSED},elementCheckboxPressed);stateElement.addState(newint[]{ComponentState.COMPONENT_STATE_DISABLED},elementCheckboxDisable);stateElement.addState(newint[]{ComponentState.COMPONENT_STATE_CHECKED},elementCheckedle)(newint[]{ComponentState.COMPONENT_STATE_EMPTY},elementCheckboxEmpty);//创建一个新的按钮并添加到布局中Checkboxcheckbox=newCheckbox(this);checkbox.setText("Checkbox");checkbox.setTextSize(50);//设置按钮的状态,如果为false,表示状态为COMPONENT_STATE_DISABLED//checkbox.setEnabled(false);checkbox.setButtonElement(stateElement);//checkbox点击也会有状态改变//checkbox.setBackground(stateElement);定向布局。LayoutConfiglayoutConfig=newDirectionalLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT,ComponentContainer.LayoutConfig.MATCH_CONTENT);layoutConfig.alignment=LayoutAlignment.CENTER;layoutConfig.setMargins(20,20,20,20);checkbox.setLayoutConfig(layoutboxConfig);dlSelecton(chaddon.);}本文作者:鸿蒙三方数据库联合专项组张恒。