問題描述
是否可以在用戶輸入數據時自動將字符插入到 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) {
}
});
這篇關于實時編輯用戶輸入的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!