問題描述
我有一個 EditText 視圖,我希望它將用戶的輸入格式化為電話號碼格式.例如,當用戶輸入 1234567890 時,只要輸入前 3 個數(shù)字,EditText 視圖就會動態(tài)顯示為(123) 456-7890".
I have an EditText view and I want it to format the user's input into the phone number format. For example, when the user types in 1234567890, the EditText view should dynamically show it as "(123) 456-7890" as soon as the first 3 numbers are inputted.
我在 OnCreate 中嘗試了以下操作,但它似乎對我沒有任何作用...
I tried the following in my OnCreate but it didn't seem to do anything for me...
EditText ET = (EditText) findViewById(R.id.add_number);
ET.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
如何讓用戶的輸入以電話號碼格式顯示?
How can I get the user's input to display in the phone number format?
推薦答案
我做了一個組合,我將輸入類型修改為 phone,然后我使用正則表達式刪除所有非數(shù)字字符: phonenumber = phonenumber.replaceAll("D", "");
I did a combination of things, i modified my input type to phone, then I used regex to remove all non numeric characters: phonenumber = phonenumber.replaceAll("D", "");
這篇關于為電話號碼格式化 EditText 視圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!