Flex,如何使用colorPicker在textArea中制作多彩字段?

时间:2012-02-08 01:15:35

标签: flex textarea chat

我正在尝试使用Flash Builder创建聊天。我有聊天区域(textArea组件),用于键入的字段(textInput组件)和用于更改文本颜色的colorPicker。问题是它无法改变textArea中每条消息的颜色(如果我用红色键入一条消息,另一条用蓝色等消息),我只能使用以下方法将它们全部改为:

txtHistory.setStyle("color",cp.selectedColor);

完整代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init()">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            private const SERVER:String = "rtmfp://stratus.adobe.com/";
            private const DEVKEY:String = "YOUR-DEVKEY";

            [Bindable]
            private var userName:String;

            [Bindable]
            private var connected:Boolean = false;

            private var netConnection:NetConnection;
            private var netGroup:NetGroup;

            private function init():void{
                userName = "user"+Math.round(Math.random()*1000);

                connect();

            }

            private function connect():void{
                netConnection = new NetConnection();
                netConnection.addEventListener(NetStatusEvent.NET_STATUS,netStatus);                
                netConnection.connect(SERVER+DEVKEY);
            }

            private function netStatus(event:NetStatusEvent):void{
                writeText(event.info.code);

                switch(event.info.code){
                    case "NetConnection.Connect.Success":
                        setupGroup();
                        break;

                    case "NetGroup.Posting.Notify":
                        receiveMessage(event.info.message);
                        break;

                    case "NetGroup.Connect.Success":
                        connected = true;
                        break;
                }
            }

            private function setupGroup():void{
                var spec:GroupSpecifier = new GroupSpecifier("myGroup");
                spec.postingEnabled = true;
                spec.serverChannelEnabled = true;

                netGroup = new NetGroup(netConnection,spec.groupspecWithAuthorizations());
                netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatus);

            }

            private function sendMessage(txt:String):void{
                var message:Object = new Object();
                message.text = txt;
                message.userName = txtUser.text;
                message.sender = netGroup.convertPeerIDToGroupAddress(netConnection.nearID);

                netGroup.post(message);
                receiveMessage(message);
            }

            private function receiveMessage(message:Object):void{
                writeText(message.userName +": "+message.text);

            }

            private function writeText(txt:String):void{
                txtHistory.appendText(txt+"\n");
    txtHistory.setStyle("color",cp.selectedColor);
            }



            protected function sendNow():void
            {
                sendMessage(txtMessage.text);
            }

        ]]>
    </fx:Script>

    <s:TextInput text="{userName}" x="10" bottom="10" id="txtUser"/>
    <s:TextInput left="146" right="88" bottom="10" id="txtMessage" enter="sendNow()"/>
    <s:TextArea left="10" right="10" top="10" bottom="40" id="txtHistory"/>
    <s:Button enabled="{connected}" label="Send" bottom="10" right="10" click="sendNow()" id="btnSend"/>
<mx:ColorPicker id="cp" x="10" y="221" width="29" height="29" selectedColor="#CCCCCC"/>
</s:Application>

第二个问题是即使是全色变化(txtHistory.setStyle("color",cp.selectedColor);

仅适用于txtHistory.appendText(txt+"\n");

txtHistory.text += txt+"\n";更改的情况

这件事怎么解决?我听说过htmlText,但我是Flex的新手,并不确切知道它是如何工作的。带有示例的代码对我来说是最好的答案!

提前谢谢!

0 个答案:

没有答案