实时编辑用户输入

Live editing of users input(实时编辑用户输入)

本文介绍了实时编辑用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在用户输入数据时自动将字符插入到 EditText ?

Is it possible to auto insert characters into an EditText as the user inputs data?

即如果用户输入一个长数字,例如 123456789012,这个数字是否可以在他在编辑文本框中输入时出现,但每 4 个字符有一个破折号?

I.e. if the user is entering a long number such as 123456789012, is it possible for this number to appear as he is typing it in the edit text box, but with a dash every 4th character?

因此,当您键入上面的数字时,您会看到它被输入到 EditText 框中,但看起来像这样:1234-5678-9012.

So as you type the number above you would see it being entered in the EditText box but would look like this: 1234-5678-9012.

目前我有一个应用程序,您可以在其中输入一个长数字,然后按一个按钮,它会为您插入破折号,但我很好奇是否可以在您输入时完成?

Currently I have an app where you can enter a long number and then press a button and it inserts the dashes for you, but I'm curious if it could be done as you type?

非常感谢您的帮助.

推荐答案

通过标记android,我想你是在讨论android的editText,所以你可以通过监听TextChangedListener来实现,

By tagging android, I think you are discussing about android editText, is so you can do it by listening the TextChangedListener,

已用于退格

editText.addTextChangedListener(new TextWatcher() {
            int len=0;
            @Override
            public void afterTextChanged(Editable s) { 
                String str = editText.getText().toString(); 
                 if(str.length()==4&& len <str.length()){//len check for backspace 
                    editText.append("-");
                }
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

             String str = editText.getText().toString(); 
              len = str.length();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {  
            }


        }); 

这篇关于实时编辑用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:实时编辑用户输入

基础教程推荐