国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > WEB開(kāi)發(fā) > 正文

17.8.記住用戶(hù)輸入文本框的內(nèi)容

2024-04-27 13:52:24
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
17.8.1. 問(wèn)題
我想要再用戶(hù)離開(kāi)應(yīng)用程序的時(shí)候,記住用戶(hù)輸入的TextInput 的字段。
17.8.2. 解決辦法
創(chuàng)建一個(gè)TextInput 組建的子類(lèi),當(dāng)用戶(hù)輸入的時(shí)候來(lái)使用本地共享對(duì)象存貯用戶(hù)輸入的文本值。
17.8.3. 討論
為了方便,現(xiàn)代瀏覽器都提供了記住用戶(hù)上次在公共表單區(qū)域輸入值和登陸提示,這些值在用戶(hù)再次訪(fǎng)問(wèn)的時(shí)候不用再次輸入。默認(rèn)情況,F(xiàn)lex 應(yīng)用程序沒(méi)有繼承這個(gè)非常有用的行為,但是你可以很簡(jiǎn)單的通過(guò)使用本章的例子中的自定義組件來(lái)實(shí)現(xiàn)。

這個(gè)組件繼承于TextInput 類(lèi),增加了一個(gè)靜態(tài)方法到TextInput 的API,被設(shè)計(jì)了用來(lái)記住最后一次的值. persistenceId 屬性是存貯ID 用來(lái)指定這個(gè)組件的實(shí)例。靜態(tài)方法clearStoredValues()讓你可以全局晴空所有之前存貯的值。代碼如下:
+展開(kāi)
-ActionScript
package custom
{
import flash.events.Event;
import flash.net.SharedObject;
import mx.controls.TextInput;
public class PersistentTextInput extends TextInput
{
/**
* The ID this component will use to save and later look up its
* associated value.
*/

public var persistenceId:String = null;
/**
* The SharedObject name to use for storing values.
*/

private static const LOCAL_STORAGE_NAME:String =
"persistentTextInputStorage";
/**
* Clears previously stored values for all PersistentTextInput instances.
*/

public static function clearStoredValues() :void
{
var so:SharedObject = SharedObject.getLocal(LOCAL_STORAGE_NAME);
so.clear();
}
/**
* Handles initialization of this component.
*/

override public function initialize() :void
{
super.initialize();
addEventListener(Event.CHANGE, handleChange);
restoreSavedValue();
}
/**
* Event handler function for CHANGE events from this instance.
*/

protected function handleChange(event:Event) :void
{
saveCurrentValue();
}
/**
* Restores the previously saved value associated with the
* persistenceID of with this instance.
*/

protected function restoreSavedValue() :void
{
if (persistenceId != null)
{
var so:SharedObject =
SharedObject.getLocal(LOCAL_STORAGE_NAME);
var value:String = so.data[persistenceId];
if (value != null)
{
text = value;
}
}
}
/**
* Saves the text value of this instance. Associates the value with
* the persistenceId of this instance.
*/

protected function saveCurrentValue() :void
{
if (persistenceId != null)
{
var so:SharedObject =
SharedObject.getLocal(LOCAL_STORAGE_NAME);
so.data[persistenceId] = text;
so.flush();
}
}
}
}

如下應(yīng)用程序示范了如何使用這個(gè)新組件。測(cè)試這些功能,載入應(yīng)用程序,輸入一些文字,然后關(guān)閉應(yīng)用程序。當(dāng)你再次打開(kāi)應(yīng)用程序的時(shí)候,你會(huì)看到之前輸入的一些字段會(huì)有之前你輸入的值。
+展開(kāi)
-XML
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxmlxmlns:custom="custom.*layout="vertical">
<mx:Script>
<![CDATA[
import custom.PersistentTextInput;
public function clearValues() :void
{
PersistentTextInput.clearStoredValues();
message.text = "A page refresh will reveal that the
values have not persisted.";
}

]]>
</mx:Script>
<custom:PersistentTextInput id="firstNameInput"
persistenceId="firstName" />

<custom:PersistentTextInput id="lastNameInput"
persistenceId="lastName" />

<mx:Button label="Clear Persistent Values"
click="clearValues()" />

<mx:Label id="message" />
</mx:Application>
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 芦溪县| 五台县| 万州区| 尼玛县| 鹤壁市| 灵武市| 南华县| 松原市| 平果县| 红安县| 岳阳市| 太保市| 宜州市| 成武县| 辽宁省| 彩票| 北海市| 台前县| 北安市| 湘西| 南澳县| 青海省| 江源县| 沧州市| 长岭县| 肇州县| 青岛市| 白玉县| 重庆市| 巴南区| 安徽省| 大埔区| 合山市| 鹰潭市| 汉中市| 洪雅县| 阳城县| 黄石市| 六盘水市| 托克托县| 杨浦区|