android – 格式编辑电话号码的EditText视图

android – 格式编辑电话号码的EditText视图,第1张

概述我有一个EditText视图,我希望它将用户的输入格式化为电话号码格式.例如,当用户键入1234567890时,一旦输入前3个数字,EditText视图应该动态显示为“(123)456-7890”. 我在我的OnCreate尝试了以下内容,但似乎没有为我做任何事情 EditText ET = (EditText) findViewById(R.id.add_number);ET.addTextC 我有一个EditText视图,我希望它将用户的输入格式化为电话号码格式.例如,当用户键入1234567890时,一旦输入前3个数字,EditText视图应该动态显示为“(123)456-7890”.

我在我的OnCreate尝试了以下内容,但似乎没有为我做任何事情

EditText ET = (EditText) findVIEwByID(R.ID.add_number);ET.addTextChangedListener(new PhoneNumberFormattingTextWatcher());

如何让用户的输入以手机号码格式显示?

解决方法 使用此代码,您可以自定义TextWatcher并制作任何您想要的格式:
ET.addTextChangedListener(new PhoneNumberFormattingTextWatcher() {        //we need to kNow if the user is erasing or inputing some new character        private boolean backspacingFlag = false;        //we need to block the :afterTextChanges method to be called again after we just replaced the EditText text        private boolean editedFlag = false;        //we need to mark the cursor position and restore it after the edition        private int cursorComplement;        @OverrIDe        public voID beforeTextChanged(CharSequence s,int start,int count,int after) {            //we store the cursor local relative to the end of the string in the EditText before the edition            cursorComplement = s.length()-ET.getSelectionStart();            //we check if the user ir inputing or erasing a character            if (count > after) {                backspacingFlag = true;            } else {                backspacingFlag = false;            }        }        @OverrIDe        public voID onTextChanged(CharSequence s,int before,int count) {            // nothing to do here =D        }        @OverrIDe        public voID afterTextChanged(Editable s) {            String string = s.toString();            //what matters are the phone digits beneath the mask,so we always work with a raw string with only digits            String phone = string.replaceAll("[^\d]","");            //if the text was just edited,:afterTextChanged is called another time... so we need to verify the flag of edition            //if the flag is false,this is a original user-typed entry. so we go on and do some magic            if (!editedFlag) {                //we start verifying the worst case,many characters mask need to be added                //example: 999999999 <- 6+ digits already typed                // masked: (999) 999-999                if (phone.length() >= 6 && !backspacingFlag) {                    //we will edit. next call on this textWatcher will be ignored                    editedFlag = true;                    //here is the core. we substring the raw digits and add the mask as convenIEnt                    String ans = "(" + phone.substring(0,3) + ") " + phone.substring(3,6) + "-" + phone.substring(6);                    ET.setText(ans);                    //we deliver the cursor to its original position relative to the end of the string                    ET.setSelection(ET.getText().length()-cursorComplement);                //we end at the most simple case,when just one character mask is needed                //example: 99999 <- 3+ digits already typed                // masked: (999) 99                } else if (phone.length() >= 3 && !backspacingFlag) {                    editedFlag = true;                    String ans = "(" +phone.substring(0,3) + ") " + phone.substring(3);                    ET.setText(ans);                    ET.setSelection(ET.getText().length()-cursorComplement);                }            // We just edited the fIEld,ignoring this cicle of the watcher and getting ready for the next            } else {                editedFlag = false;            }        }    });

确保将XML中的EditText Lenght限制为14个字符

<EditText    androID:ID="@+ID/editText_phone"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:inputType="phone"    androID:lines="1"    androID:maxLength="14"/>
总结

以上是内存溢出为你收集整理的android – 格式编辑电话号码的EditText视图全部内容,希望文章能够帮你解决android – 格式编辑电话号码的EditText视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1132941.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存