从动态文本字段中删除字符

时间:2014-06-25 13:44:03

标签: actionscript-3

我对脚本非常陌生。我正在尝试为我的Android手机拨号。

我在舞台上有一个动态文本框和10个按钮,(0-9)。我能够编程按钮,使用(+ =)将数字添加到文本字段。然后,我编写了一个删除按钮来删除数字。它一次删除所有数字,这是我不想要的。

我希望删除最后放入的字符。另外,我想将数字分组为3,3,4(000 000 0000)。
关于如何做这两件事的任何想法都将不胜感激。

谢谢你帮助我。我能够添加你给我的代码和删除最后一个字符工作得很好但我已经尝试输入代码进行分组和间隔数字除了正确的方式我想。我如何让这个工作,确保它会有一点帮助

one_btn.addEventListener(MouseEvent.CLICK,n1click);

function n1click(event:MouseEvent):void {     如果(n1click)     input_txt.text + =(“1”); } input_txt.addEventListener(Event.CHANGE,onFieldChange); //一旦我们的文本字段被更改,此侦听器将触发

function onFieldChange(e:Event):void {
    if(field.length % (groupBy + 1) == 0) { //if the modulo of our groupBy + 1 is 0 => we have 4, 8, 12, etc.. chars in our field
        field.text = field.text.substr(0, field.text.length - 1) + " " + field.text.charAt(field.text.length - 1); //insert the space before the last character
        field.setSelection(field.text.length, field.text.length); //set the caret at the end of the text
    }
}

1 个答案:

答案 0 :(得分:0)

你有没有尝试过什么?它可以通过不同的方式实现,这里有一个:

var field:TextField = this.field; //field is on the timeline, just retrieving the instance
var groupBy:uint = 3; //let's use this to show groups of 3 numbers

field.addEventListener(Event.CHANGE, onFieldChange); //once our textfield is changed, this listener will fire

function onFieldChange(e:Event):void {
    if(field.length % (groupBy + 1) == 0) { //if the modulo of our groupBy + 1 is 0 => we have 4, 8, 12, etc.. chars in our field
        field.text = field.text.substr(0, field.text.length - 1) + " " + field.text.charAt(field.text.length - 1); //insert the space before the last character
        field.setSelection(field.text.length, field.text.length); //set the caret at the end of the text
    }
}

function deleteLastChar():void {
    if(field.text.charAt(field.text.length - 2) == " ") { //if it is the first char of the group (=means it has a space before)
        field.text = field.text.slice(0, -2); //remove the last character and the space
    }
    else {
        field.text = field.text.slice(0, -1); //remove only the last char
    }
    field.setSelection(field.text.length, field.text.length); //and once again set the caret (if necessary)
}