【问题标题】:Remove a character from a dynamic textfield [closed]从动态文本字段中删除一个字符[关闭]
【发布时间】:2014-06-25 13:44:03
【问题描述】:

我对脚本非常陌生。我正在尝试为我的 android 手机制作拨号器。

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

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

感谢您帮助我。我能够添加您给我的代码,并且删除最后一个字符效果很好,但我尝试输入代码以对数字进行分组和间隔,除了我猜的正确方式。我如何让它工作,我相信它会在一些帮助下完成

one_btn.addEventListener(MouseEvent.CLICK, n1click);

函数 n1click(event:MouseEvent):void{ 如果(n1点击) 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
    }
}

【问题讨论】:

  • “安德烈席尔瓦”是谁?如果您对回复有任何疑问,请在回复下方发表评论,而不是在您的问题下方。

标签: actionscript-3


【解决方案1】:

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

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)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-04
    • 2011-07-05
    • 2012-09-12
    相关资源
    最近更新 更多