Android - 我想從用戶獲取一個數字輸入到EditText中 - 它需要用空格分隔 - 每4個字符。 示例:123456781234 - > 1234 5678 1234android format edittext在每4個字符後顯示空格
這僅用於視覺目的。但是,我需要沒有空間的字符串作進一步的使用。
我能做到這一點的最簡單方法是什麼?
Android - 我想從用戶獲取一個數字輸入到EditText中 - 它需要用空格分隔 - 每4個字符。 示例:123456781234 - > 1234 5678 1234android format edittext在每4個字符後顯示空格
這僅用於視覺目的。但是,我需要沒有空間的字符串作進一步的使用。
我能做到這一點的最簡單方法是什麼?
您需要使用TextWatcher來實現視覺目的空間。
並通過空間使用任何簡單分割字符串邏輯明智加入其背部或遍歷每個字符整個字符串,並從字符串消除(char) 32
爲@waqas指出的那樣,你需要使用一個TextWatcher,如果你的目的是在用戶輸入數字時發生這種情況。這是一個潛在的方式,你可以實現的空間:
StringBuilder s;
s = new StringBuilder(yourTxtView.getText().toString());
for(int i = 4; i < s.length(); i += 5){
s.insert(i, " ");
}
yourTxtView.setText(s.toString());
每當你需要得到的字符串沒有空格這樣做:
String str = yourTxtView.getText().toString().replace(" ", "");
這裏是一個小的幫助作用。爲了您的例子中,你將與
addPadding(" ", "123456781234", 4);
/**
* @brief Insert arbitrary string at regular interval into another string
*
* @param t String to insert every 'num' characters
* @param s String to format
* @param num Group size
* @return
*/
private String addPadding(String t, String s, int num) {
StringBuilder retVal;
if (null == s || 0 >= num) {
throw new IllegalArgumentException("Don't be silly");
}
if (s.length() <= num) {
//String to small, do nothing
return s;
}
retVal = new StringBuilder(s);
for(int i = retVal.length(); i > 0; i -= num){
retVal.insert(i, t);
}
return retVal.toString();
}
稱呼它是這個editext信用卡?
首先創建計數變量
int count = 0;
那麼把它放進你的OnCreate(活動)/ onviewcreated(片段)
ccEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (count <= ccEditText.getText().toString().length()
&&(ccEditText.getText().toString().length()==4
||ccEditText.getText().toString().length()==9
||ccEditText.getText().toString().length()==14)){
ccEditText.setText(ccEditText.getText().toString()+" ");
int pos = ccEditText.getText().length();
ccEditText.setSelection(pos);
}else if (count >= ccEditText.getText().toString().length()
&&(ccEditText.getText().toString().length()==4
||ccEditText.getText().toString().length()==9
||ccEditText.getText().toString().length()==14)){
ccEditText.setText(ccEditText.getText().toString().substring(0,ccEditText.getText().toString().length()-1));
int pos = ccEditText.getText().length();
ccEditText.setSelection(pos);
}
count = ccEditText.getText().toString().length();
}
});
變化的文字直播,同時打字一些什麼困難。我們應該處理以下問題。
a。光標位置 b。我們應該允許用戶刪除輸入的文本。
以下代碼處理這兩個問題。
添加TextWatcher到EditText上,並從文本 「afterTextchanged()」,寫你的邏輯
字符串str = 「」; int strOldlen = 0;
@Override
public void afterTextChanged(Editable s) {
str = edtAadharNumber.getText().toString();
int strLen = str.length();
if(strOldlen<strLen) {
if (strLen > 0) {
if (strLen == 4 || strLen == 9) {
str=str+" ";
edtAadharNumber.setText(str);
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
}else{
if(strLen==5){
if(!str.contains(" ")){
String tempStr=str.substring(0,strLen-1);
tempStr +=" "+str.substring(strLen-1,strLen);
edtAadharNumber.setText(tempStr);
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
}
}
if(strLen==10){
if(str.lastIndexOf(" ")!=9){
String tempStr=str.substring(0,strLen-1);
tempStr +=" "+str.substring(strLen-1,strLen);
edtAadharNumber.setText(tempStr);
edtAadharNumber.setSelection(edtAadharNumber.getText().length());
}
}
strOldlen = strLen;
}
}else{
return;
}
}else{
strOldlen = strLen;
Log.i("MainActivity ","keyDel is Pressed ::: strLen : "+strLen+"\n old Str Len : "+strOldlen);
}
}
}
這裏我想爲每四個字符添加空間。在添加第一個空格之後,文本的長度是5.所以下一個空格是在9個字符之後。
如果(strlen的== 4 || STRLEN == 9)
edtAadharNumber.setSelection(edtAadharNumber.getText()。length());
請將您的評論從代碼中移除。 – 2016-06-09 05:29:12
當我嘗試刪除文本時,它一次只能刪除4個字符。 – Roon13 2017-01-30 11:46:56
格式的文本是000 000 0000
android edittext textwatcher format phone number like xxx-xxx-xx-xx
public class PhoneNumberTextWatcher implements TextWatcher {
private static final String TAG = PhoneNumberTextWatcher.class
.getSimpleName();
private EditText edTxt;
private boolean isDelete;
public PhoneNumberTextWatcher(EditText edTxtPhone) {
this.edTxt = edTxtPhone;
edTxt.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL) {
isDelete = true;
}
return false;
}
});
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
if (isDelete) {
isDelete = false;
return;
}
String val = s.toString();
String a = "";
String b = "";
String c = "";
if (val != null && val.length() > 0) {
val = val.replace(" ", "");
if (val.length() >= 3) {
a = val.substring(0, 3);
} else if (val.length() < 3) {
a = val.substring(0, val.length());
}
if (val.length() >= 6) {
b = val.substring(3, 6);
c = val.substring(6, val.length());
} else if (val.length() > 3 && val.length() < 6) {
b = val.substring(3, val.length());
}
StringBuffer stringBuffer = new StringBuffer();
if (a != null && a.length() > 0) {
stringBuffer.append(a);
if (a.length() == 3) {
stringBuffer.append(" ");
}
}
if (b != null && b.length() > 0) {
stringBuffer.append(b);
if (b.length() == 3) {
stringBuffer.append(" ");
}
}
if (c != null && c.length() > 0) {
stringBuffer.append(c);
}
edTxt.removeTextChangedListener(this);
edTxt.setText(stringBuffer.toString());
edTxt.setSelection(edTxt.getText().toString().length());
edTxt.addTextChangedListener(this);
} else {
edTxt.removeTextChangedListener(this);
edTxt.setText("");
edTxt.addTextChangedListener(this);
}
}
}
@ ARIO的回答更清潔的版本,遵循DRY原則:
private int prevCount = 0;
private boolean isAtSpaceDelimiter(int currCount) {
return currCount == 4 || currCount == 9 || currCount == 14;
}
private boolean shouldIncrementOrDecrement(int currCount, boolean shouldIncrement) {
if (shouldIncrement) {
return prevCount <= currCount && isAtSpaceDelimiter(currCount);
} else {
return prevCount > currCount && isAtSpaceDelimiter(currCount);
}
}
private void appendOrStrip(String field, boolean shouldAppend) {
StringBuilder sb = new StringBuilder(field);
if (shouldAppend) {
sb.append(" ");
} else {
sb.setLength(sb.length() - 1);
}
cardNumber.setText(sb.toString());
cardNumber.setSelection(sb.length());
}
ccEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String field = editable.toString();
int currCount = field.length();
if (shouldIncrementOrDecrement(currCount, true)){
appendOrStrip(field, true);
} else if (shouldIncrementOrDecrement(currCount, false)) {
appendOrStrip(field, false);
}
prevCount = cardNumber.getText().toString().length();
}
});
假設你知道String的最後長度,你可以im plement一個TextWatcher
這樣:
override fun setUp(view: View?) {
editText.addTextChangedListener(object : TextWatcher{
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence, p1: Int, p2: Int, p3: Int) {
if(p2 == 0 && (p0.length == 4 || p0.length == 9 || p0.length == 14))
editText.append(" ")
}
override fun afterTextChanged(p0: Editable?) {
}
})
您只需添加一個空間的每個4位數塊。 p2 == 0
是爲了保證用戶沒有刪除,否則他/她會得到庫存。
該代碼位於Kotlin中,您可以在Java中以完全相同的方式執行該代碼。
此代碼應該確實使用某種循環,而不是像這樣的硬編碼長度 – Lukas1 2018-02-20 09:10:44