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

首頁 > 網站 > WEB開發 > 正文

17.8.記住用戶輸入文本框的內容

2024-04-27 13:52:24
字體:
來源:轉載
供稿:網友
17.8.1. 問題
我想要再用戶離開應用程序的時候,記住用戶輸入的TextInput 的字段。
17.8.2. 解決辦法
創建一個TextInput 組建的子類,當用戶輸入的時候來使用本地共享對象存貯用戶輸入的文本值。
17.8.3. 討論
為了方便,現代瀏覽器都提供了記住用戶上次在公共表單區域輸入值和登陸提示,這些值在用戶再次訪問的時候不用再次輸入。默認情況,Flex 應用程序沒有繼承這個非常有用的行為,但是你可以很簡單的通過使用本章的例子中的自定義組件來實現。

這個組件繼承于TextInput 類,增加了一個靜態方法到TextInput 的API,被設計了用來記住最后一次的值. persistenceId 屬性是存貯ID 用來指定這個組件的實例。靜態方法clearStoredValues()讓你可以全局晴空所有之前存貯的值。代碼如下:
+展開
-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();
}
}
}
}

如下應用程序示范了如何使用這個新組件。測試這些功能,載入應用程序,輸入一些文字,然后關閉應用程序。當你再次打開應用程序的時候,你會看到之前輸入的一些字段會有之前你輸入的值。
+展開
-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>
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 棋牌| 荔浦县| 丽江市| 哈密市| 神农架林区| 祁门县| 云梦县| 金门县| 大新县| 高雄市| 绍兴市| 舟曲县| 陇川县| 云霄县| 册亨县| 泰和县| 平乡县| 万源市| 晴隆县| 衡南县| 麻栗坡县| 潜江市| 昆山市| 西乌珠穆沁旗| 汉阴县| 宜春市| 日喀则市| 封丘县| 德令哈市| 根河市| 建平县| 奉贤区| 云和县| 瑞金市| 崇义县| 满洲里市| 陈巴尔虎旗| 高碑店市| 彭水| 高青县| 潜江市|