Keyboard-focus on Texbox in ActionScript - actionscript-3

I made a login form on Flex and set the focus on the first textbox.
The textbox is highlighted, but I have to click on it in order to be able to write inside it.
How can I make it takes keyboard input directly without clicking? I mean directly after loading the page.
<fx:Script>
<![CDATA[
private function init():void {
trace("Authentication View init");
ExternalInterface.call('function browserFocus(){document.getElementById(\'${application}\').focus();}');
this.txtUsername.setFocus();
this.txtUsername.setFocus();
}
]]>
</fx:Script>
<s:Panel x="353" y="164" width="250" height="200">
<s:TextInput id="txtUsername" x="103" y="49" focusEnabled="true"/>
<s:TextInput x="103" y="79"/>
<s:Label x="26" y="49" text="Username"/>
<s:Label x="26" y="79" text="Password"/>
</s:Panel>

Set focus like following inside init() function.
txtUsername.setFocus();
And Apply following code inside html-template/index.template.html. before HTML end tag.
<script type="text/javascript">
function onFlexInitialized()
{
//alert("onFlexInitialized");
<!-- Force the browser to set flex app with focus -->
document.getElementById("${application}").focus();
}
</script>

I solved it by adding this inside the body tag in html-template/index.template.html.
onLoad="window.document.Login.focus();"
and it worked corrctly. Thanks everyone :)

Index-template:
<div id="flashContent">
<script type="text/javascript">
function setFocus(){
window.document.getElementById("APPNAME").focus();
$('#txtUsername').focus();
$('#txtUsername').focusEnables=true;
}
</script>
</div>
Application:
private function init():void{
if (ExternalInterface.available) {
ExternalInterface.call('setFocus');
}
}

Related

How do I get the LinkButton in flex to underline on mouse over

I am new to using flex. I have a linkButton and I need it to underline when I hover the mouse over. I think it can be done by setstyle() but I don't know the syntax and how it works. I looked around but it quite find anything useful related to this.
Any help is much appreciated.
The syntax to use is:
buttonObject.setStyle("property", "value");
A complete example:
<fx:Script>
<![CDATA[
import mx.controls.Alert;
protected function linkbutton1_clickHandler(event:MouseEvent):void
{
Alert.show('LinkButton selected!');
}
protected function linkbutton1_mouseOverHandler(event:MouseEvent):void
{
btn.setStyle("textDecoration", "underline");
}
protected function linkbutton1_mouseOutHandler(event:MouseEvent):void
{
btn.setStyle("textDecoration", "none");
}
]]>
</fx:Script>
<mx:LinkButton id="btn" label="LinkButton control" color="#0000FF" fontWeight="bold" rollOverColor="#FFFFFF"
mouseOver="linkbutton1_mouseOverHandler(event)"
mouseOut="linkbutton1_mouseOutHandler(event)"
click="linkbutton1_clickHandler(event)"/>
See more at Display text as hyperlink in Flex and http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/LinkButton.html

AS3 TextArea click handling

I am having trouble with handling a click event on a TextArea. I'm developing a mobile app using Flash Mobile. I would like to display a default text in the area and make it disappear when the user selects the field.
The issue is, that the event is thrown only when I click on the border of the TextArea. It never happens when a selection cursor is active. I also tried to add a false editable property to notesInput and set it to true in the handler and it didn't help.
private function notesClickHandler(event:Event):void{
notesInput.text = "";
notesInput.removeEventListener(MouseEvent.CLICK, notesClickHandler);
form.invalidElements;
}
<TextArea id="notesInput" text="Poznámky.."
height="150" width="100%"
verticalScrollPolicy="auto"/>
Thank you guys for your time and your help.
You can use prompt text which will fulfill your requirement:
<s:TextArea id="notesInput"
prompt="Default Text"
height="150" width="100%"
verticalScrollPolicy="auto"/>
It will not clear the default text when you focus in. But you can make that happen by clearing prompt text when you focus in the TextArea.
<fx:Script><![CDATA[
private function onFocusIn():void
{
notesInput.prompt = "";
}
private function onFocusOut():void
{
notesInput.prompt = "Default Text";
}
]]></fx:Script>
<s:TextArea id="notesInput"
prompt="Default Text"
height="150" width="100%"
verticalScrollPolicy="auto"
focusIn="onFocusIn()"
focusOut="onFocusOut()"/>
For that you can use an focusIn event like this :
private function text_area_focusInHandler(event:FocusEvent):void
{
text_area.text = ''
}
<s:TextArea id="text_area" x="10" y="10" focusIn="text_area_focusInHandler(event)" text="default text"/>

add vertical scrollbar to flex tooltip

I want to add scrollbar to the flex tootip. So i created custom tool tip. Below is the code snippet. Problem i am facing is that as soon as i move the mouse on the custom component tooltip, the custom component tooltip disppear. i have alse set the ToolTipManager.hideDelay = Infinity. I want to use the scrollbar of the cutom tool tip.
I do not want the custom tool top to hide till i move the mouse outside the custom tool tip component.Currently it hide itself once i move the mouse outside the label. How to control the destroy of tool tip.
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="100%"
height="100%"
implements="mx.core.IToolTip">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private var _bodyText:String = "";
[Bindable]
public function get bodyText():String
{
return _bodyText;
}
public function set bodyText(value:String):void
{
_bodyText = value;
text = value;
}
private var _text:String;
public function get text():String
{
return _text;
}
public function set text(value:String):void
{
_text = value;
}
]]>
</fx:Script>
<s:Scroller width="100%" height="200">
<s:RichEditableText text="{bodyText}" width="100%" height="100%" color="red"/>
</s:Scroller>
</s:Group>
and i add this cutom tooltip on mx:label component on toolTipCreate event.
protected function label1_toolTipCreateHandler(event:ToolTipEvent):void
{
ScrollableToolTip ptt = new ScrollableToolTip();
ptt.bodyText = data.notes;
ptt.height = 300;
ptt.width = 100;
event.toolTip = ptt;
}
Any pointers..
Thanks
Raj
Instead of relying on the flex tooltip, you may want to add a popup on mouse roll over and deal with it that way, ofcoz you have to deal with the positioning of the popup later on, but I think this will give you a lot of flexibility later on.

ItemRenderer Objects Changing Properties

Apologies for the vague title. I could not think of a better way to word it in so few words.
Basically, I am creating a custom ItemRenderer (IR). The IR has a label on the left side and an icon on the right side. The icon on the right side is dynamic (can be either an add or remove icon or nothing at all). This works beautifully and gives me the control I need over it.
Now, the problem is that when I scroll the list in my mobile application, the icons change.
How it should look:
How it looks after scrolling using a finger (or mouse in emulator) by dragging on Test3:
As you can see, the icons change but the labels do not. I have dragEnabled, dropEnabled, and dragMoveEnabled all set to false on my Spark List component. The only time the icon selection runs is on creationComplete, so there is no way it is choosing a different icon at some point. I can also verify that the data itself is what it should be after this change occurs.
Here is the MXML that creates the item:
<s:HGroup width="100%" verticalAlign="middle" left="{this.sideSpacing}" right="{this.sideSpacing}" top="{this.sideSpacing}" bottom="{this.sideSpacing}">
<s:Label id="text" width="100%"/>
<s:Image id="actionIcon" buttonMode="true" click="actionIconClick( event );">
<s:filters>
<s:DropShadowFilter alpha=".45" angle="90" distance="3" blurX="3" blurY="3" quality="3"/>
</s:filters>
</s:Image>
</s:HGroup>
<s:Rect width="100%" height="1" bottom="0" alpha=".1">
<s:fill>
<s:SolidColor color="#000000"/>
</s:fill>
</s:Rect>
And the possibly over-elaborate AS3 for selecting which icon should be displayed:
private function creationComplete( e:Event ):void {
if ( this.data.actionIcon != null ) {
this.actionIconType = this.data.actionIcon;
if ( this.data.actionIcon == ACTION_ICON_ADD ) {
this.actionIcon.source = this.addBitmapSource;
}
else if ( this.data.actionIcon == ACTION_ICON_REMOVE ) {
this.actionIcon.source = this.removeBitmapSource;
}
else {
this.actionIcon.source = null;
this.actionIcon.visible = false;
this.actionIcon.includeInLayout = false;
}
}
else {
this.actionIcon.source = null;
this.actionIcon.visible = false;
this.actionIcon.includeInLayout = false;
}
}
What could be causing this issue?
Basically, you need to update your renderer's label and icon when the dataChange event is fired. CreationComplete is only fired once. The list isn't really scrolled, just that the data in the itemRenderers change; causing it to look like things are scrolling. I call this renderer recycling.
Here is a component I created for a mobile app that does what you want. It displays a label and an icon (AKA the Decorator). When scrolling the label and icon are both updated. You can use a very similar approach.
<?xml version="1.0" encoding="utf-8"?>
<s:IconItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
dataChange="onDataChange(event)" alpha=".7" width="100%" cacheAsBitmap="true">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.dotcomit.magondaMaze.managers.BoardDataManager;
import com.dotcomit.magondaMaze.managers.StatManager;
import mx.events.FlexEvent;
[Embed(source="assets/images/chooseLevel/completed78x78.png")]
private var completedImage:Class;
public var statManager :StatManager = StatManager.instance;
protected function onDataChange(event:FlexEvent):void
{
var itemAsXML :XML = data as XML;
var results :String = itemAsXML.#id + '. ' + itemAsXML.#title;
label = results;
if( statManager.isBoardComplete(itemAsXML.#id)){
this.decorator = completedImage;
} else {
this.decorator = null;
}
}
]]>
</fx:Script>
</s:IconItemRenderer>
I'll also add that the IconItemRenderer component--which my code above extends--is designed to do exactly what you need. So, you may not have to re-write the wheel so to speak.

Clicked SWF/IMAGE position to center : FLEX

Iam trying to make clicked position to center in flex.
My code is
<fx:Declarations>
<s:Parallel id="transformer" target="{swe}">
<s:Scale id="scaleby" scaleXBy="0.2" scaleYBy="0.2" autoCenterTransform="false"/>
</s:Parallel>
</fx:Declarations>
<s:Group width="500" height="350" clipAndEnableScrolling="true">
<s:SWFLoader source="CasuarinaBigMap.swf" width="500" height="350" id="swe" click="swe_clickHandler(event)"/>
</s:Group>
protected function swe_clickHandler(event:MouseEvent):void
{
scaleby.transformX = event.mouseX;
scaleby.transformY = event.mouseY;
transformer.play();
}
My qustion is
How can I make clicked point pan into the center of the box?
Pls help.
Thanks.
This ought to do the trick:
<fx:Declarations>
<s:Move id="moveEffect" />
</fx:Declarations>
<s:Group id="mapContainer" width="300" height="300" clipAndEnableScrolling="true"
click="pan(event.localX, event.localY)">
<s:Image id="map" source="#Embed('bigMap.png')" />
</s:Group>
'localX' and 'localY' are the mouse's 'x' and 'y' position relative to the mapContainer.
And now the pan() method:
private function pan(mouseX:Number, mouseY:Number):void {
//calculate the offset from mapContainer's center
var diffX:Number = mapContainer.width/2 - mouseX;
var diffY:Number = mapContainer.height/2 - mouseY;
//move the map through the move effect
moveEffect.xFrom = map.x;
moveEffect.yFrom = map.y;
moveEffect.xTo = diffX;
moveEffect.yTo = diffY;
moveEffect.play([map]);
}
Try using a Move effect instead of a Scale effect.