问题
I want to add search icon inside the spark TextInput control. Is there a way by which I can extend the TextInput control and add a child to it.
Thanks
回答1:
You shouldn't extend TextInput
itself. The main power of Spark architecture is skinning possibility. You can create your own skin based on standard TextInputSkin
and place icon there. I think there will not any problems.
回答2:
I faced the same problem wanting to get a search icon in a spark TextInput. It was very simple to copy the existing spark skin and add the icon to it. Here's how:
- create a skins folder in your workspace if you don't already have one
- select that folder in FlashBuilder, then right click on it, and from the menu that appears select New > MXML Skin
- Select "Create as copy of" in the window that appears if not already selected by default.
- In the Host Component field, type TextInput, and you should see an option appear to select spark.skins.spark.TextInputSkin.
- Enter a name (e.g. TextInputSkinWithPromptIcon) for your component and click Finish
- Open this file, which should appear in your skins folder now.
The last section in the skin file is
<!-- text -->
. AFTER this section, create a new section for<!-- search icon -->
that includes the following (note: this will be the last section in the skin):<s:Image id="iconDisplay" source="@Embed('path/to/image/file/MY_SEARCH_IMAGE.png')" mouseEnabled="false" mouseChildren="false" visible.normal="false" visible.normalWithPrompt="true"/>
Modify the exclusions arrays that appear earlier in the file so it appears as follows (note: the following code already exists in the file, just add
iconDisplay
to it as shown):/* Define the skin elements that should not be colorized. */ static private const exclusions:Array = ["background", "textDisplay", "promptDisplay", "iconDisplay", "border"];
/* exclusions before Flex 4.5 for backwards-compatibility purposes */ static private const exclusions_4_0:Array = ["background", "textDisplay", "promptDisplay", "iconDisplay",];
Go your application code where you have the TextInput component, and link it to the skin using:
<s:TextInput ... skinClass="path.to.skins.TextInputSkinWithPromptIcon" prompt=" "/>
- Make sure you have a prompt included in the TextInput component in step 9 as shown. I didn't want text there, so I simply included a prompt that is a blank space. You can use whatever you want, but there must be SOMETHING in the prompt field (otherwise the icon doesn't get displayed).
- That's it! Run it...
来源:https://stackoverflow.com/questions/5963475/add-search-icon-inside-spark-textinput-in-flex