26.5.支持IME设备

26.5.1. 问题
我想分发使用日文,中文或韩文等多字节字符的应用程序
26.5.2. 解决办法
使用Capabilities类检测输入法编辑器,使用IME类控制如何与Flex应用程序交互。
26.5.3. 讨论
东方的语言如汉字就是以象形字组成而不是用拉丁字符组成。拉到语言的字符是有限的,可被轻松的映射到键盘上。但这方法对于东方语言就不可能,因为这需要成千上万个键盘按键。

输入法编辑器(IMEs)是软件工具,允许字符由多个按键组合而成。IME是运行在操作系统级别,在Flash Player外部。

Capabilities类有个属性叫hasIME,你可以使用它检测用户系统是否安装有IME。使用flash.system.IME对象检测是否已启动IME以及设置了什么转换模式。下面的例子测试IME,如果找到,启动IME并设置转换模式:
+展开
-ActionScript
private function detectIME():void
{
if (Capabilities.hasIME == true)
{
output.text = "Your system has an IME installed.\n";
if (flash.system.IME.enabled == true)
{
output.text += "Your IME is enabled. and set to " + flash.system.IME.conversionMode;
}
else
{
output.text += "Your IME is disabled\n";
try
{
flash.system.IME.enabled = true;
flash.system.IME.conversionMode = IMEConversionMode.JAPANESE_HIRAGANA;
output.text += "Your IME has been enabled successfully";
}
catch (e:Error)
{
output.text +="Your IME could not be enabled.\n"
}
}
}
else output.text = "You do not have an IME installed.\n";
}

当尝试创作IME设置时必须使用try...catch代码块,因为如果IME不支持指定设置就会发生错误。

有时候你可能希望禁用IME,比如某个文本框只输入数字。这时你可以在组件获得焦点时触发一个函数禁用IME,当组件失去焦点时重新启动IME:
+展开
-XML
<mx:Script>
<[CDATA[
private function enableIME(enable:Boolean):void
{
if (Capabilities.hasIME)
{
try
{
flash.system.IME.enabled = enable;
trace("IME " + (enable ? "enable" : "disable"));
}
catch (e:Error)
{
Alert.show("Could not " (enable ? "enable" : "disable") + " IME");
}
}
}
]]>

</mx:Script>
<mx:VBox horizontalCenter="0verticalCenter="0" >
<mx:TextInput id="numericInputfocusIn="enableIME(false)"
focusOut="enableIME(true)" />

<mx:TextInput id="textInput" />
</mx:VBox>

如果你想知道用户是否组合一个字符,你可以监听System.ime对象事件:
+展开
-ActionScript
System.ime.addEventListener(IMEEvent.IME_COMPOSITION, onComposition);

加支付宝好友偷能量挖...


评论(0)网络
阅读(90)喜欢(0)flash/flex/fcs/AIR