Android - 我想从用户输入一个数字输入到EditText - 它需要用空格分隔 - 每4个字符。示例:123456781234 - > 1234 5678 1234
这仅用于视觉目的。但是我需要没有空格的字符串以供进一步使用。
我能做到这一点最简单的方法是什么?
你需要使用TextWatcher来实现视觉目的空间。
并使用空间逻辑的任何简单拆分字符串将其连接回来或每个字符明智地循环整个字符串并从字符串中消除(char) 32
这是信用卡的edittext? 首先创建计数变量
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();
}
});
正如@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时,请执行以下操作:
String str = yourTxtView.getText().toString().replace(" ", "");
文本格式为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);
}
}
}
这是一个小帮助功能。对于你的例子,你可以用它来调用它
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();
}
在打字时更改实时文本是一些困难。我们应该处理以下问题。
一个。光标位置b。我们应该允许用户删除输入的文本。
以下代码处理这两个问题。
@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);
}
}
}
清洁版的@ 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();
}
});
简单的答案
YourEditText.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) {
int len=s.toString().length();
if (before == 0 && (len == 4 || len == 9 || len == 14 ))
YourEditText.append(" ");
}
@Override
public void afterTextChanged(Editable s) {
}
});
假设您知道String的最终长度,您可以这样实现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完全相同的方式完成。